Android Open Source - AndroidFinally Service On Bind






From Project

Back to project page AndroidFinally.

License

The source code is released under:

GNU General Public License

If you think the Android project AndroidFinally listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.androidfinallyapp;
/*from  w  w w  . j ava 2  s.co  m*/
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class ServiceOnBind extends Service {

  private static final String TAG = "ServiceOnBind";
  public SimpleBinder sBinder;
  private NotificationManager nManager;
  private Notification notification;
  private static final int ID = 1;

  public class SimpleBinder extends Binder {

    public ServiceOnBind getService() {
      return ServiceOnBind.this;
    }

    public int add(int a, int b) {
      Log.i(TAG, "simpleBinder add func");
      return a + b;
    }
  }

  @Override
  public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    Log.i(TAG, "onBind");
    showNotification();

    return sBinder;
  }

  @SuppressWarnings("deprecation")
  private void showNotification() {
    // TODO Auto-generated method stub
    notification.icon = R.drawable.ic_launcher;// ???????????
    notification.tickerText = "????????????????"; // ????????????????
    long when = System.currentTimeMillis();
    notification.when = when; // ??????????????
    // notification.sound =
    // Uri.parse("android.resource://com.sun.alex/raw/dida"); // ?????????
    // notification.flags = Notification.FLAG_NO_CLEAR; //
    // ?????????????????????,??????????????????????
    notification.flags = Notification.FLAG_ONGOING_EVENT; // ?????????????????????,?????????????????????
    // notification.flags |= Notification.FLAG_AUTO_CANCEL; //
    // ????????????????????????
    // notification.flags |= Notification.FLAG_INSISTENT; //
    // ??????????????????????????????
    // notification.defaults = Notification.DEFAULT_SOUND; // ?????????????
    // notification.defaults = Notification.DEFAULT_VIBRATE;// ?????????
    // notification.defaults = Notification.DEFAULT_ALL; // ??????????
    notification.defaults = Notification.DEFAULT_ALL; // ??????????????????

    notification.setLatestEventInfo(this, "??", "??????", PendingIntent
        .getActivity(this, 0, new Intent(this,
            NotificationActivity.class), 0));// ???????????????

    // ???activity?????????????????
    // nManager.notify(ID, notification);
    // service???????????????????
    startForeground(ID, notification);

  }

  @Override
  public void onCreate() {
    // TODO Auto-generated method stub

    super.onCreate();
    sBinder = new SimpleBinder();

    nManager = (NotificationManager) this
        .getSystemService(Context.NOTIFICATION_SERVICE);
    notification = new Notification();
    Log.i(TAG, "onCreate");
  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    Log.i(TAG, "onStartCommand");
    return super.onStartCommand(intent, flags, startId);
  }

  @Override
  public void onDestroy() {
    // TODO Auto-generated method stub
    Log.i(TAG, "onDestroy");
    super.onDestroy();
  }

  @Override
  public void onLowMemory() {
    // TODO Auto-generated method stub
    Log.i(TAG, "onLowMemory");
    super.onLowMemory();
  }

  @Override
  public boolean onUnbind(Intent intent) {
    // TODO Auto-generated method stub
    Log.i(TAG, "onUnbind");
    // ???activity???????????????
    // nManager.cancel(ID);
    stopForeground(true);
    return super.onUnbind(intent);
  }

  @Override
  public void onRebind(Intent intent) {
    // TODO Auto-generated method stub
    Log.i(TAG, "onRebind");
    super.onRebind(intent);
  }

}




Java Source Code List

com.example.androidfinallyapp.MainActivity.java
com.example.androidfinallyapp.NotificationActivity.java
com.example.androidfinallyapp.ServiceOnBind.java
com.example.androidfinallyapp.ServiceOnStartCommand.java
com.example.androidfinallyapp.SimpleApplication.java
com.example.androidfinallyapp.StartActivity.java
com.example.hellojni.HelloJni.java