Android 4.0以前:

1: 普通的notification

private static final int NOTIFY_ID = 0;notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);    private void showNotification(Store store) {        Notification notification = new Notification();        notification.flags |= Notification.FLAG_SHOW_LIGHTS;        notification.flags |= Notification.FLAG_AUTO_CANCEL;        notification.defaults = Notification.DEFAULT_ALL;        notification.icon = R.drawable.ic_launch;        notification.when = System.currentTimeMillis();        Intent intent = new Intent(this,AlarmActivity.class);        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);         intent.putExtra("store", store);        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);//FLAG_ONE_SHOT         //Change the name of the notification here        notification.setLatestEventInfo(this, store.getStoreName()+"("+store.getDistance()+")", store.getAddress(), contentIntent);        notificationManager.notify(NOTIFY_ID, notification);    }

2: 将服务service设置为前台notification

public class MyService extends Service {    @Override    public void onCreate() {        super.onCreate();        setServiceForground();    }    @SuppressWarnings("deprecation")    public void setServiceForground() {        Notification notification = new Notification(R.drawable.ic_launcher,        "my_service_name", System.currentTimeMillis());
    //要添加newtask PendingIntent p_intent
= PendingIntent.getActivity(this, 0, new Intent( this, MainActivity.class), 0); notification.setLatestEventInfo(this, "MyServiceNotification", "MyServiceNotification is Running!", p_intent); startForeground(0x1982, notification); }
@Override
public IBinder onBind(Intent intent) { return null; }}

Android 4.0 以后:

将服务service设置为前台notification

public class MyService extends Service {    @Override    public void onCreate() {        super.onCreate();        setServiceForground();    }    @Override    public void onDestroy() {        super.onDestroy();    }    public void setServiceForground() {        Notification.Builder build = new Notification.Builder(this);//      PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(//            this, MainActivity.class), 0);//      build.setContentIntent(contentIntent);        build.setSmallIcon(R.drawable.ic_launcher);            build.setContentTitle("MyServiceNotification");        build.setContentText("MyServiceNotification is Running!");        startForeground(0x1982, build.build());    }    @Override    public IBinder onBind(Intent intent) {        return null;    }}

更多相关文章

  1. Android上传文件到Django服务器
  2. Android(安卓)TextView 给文本中指定片段添加自定义点击事件
  3. Android(安卓)M 去除抽屉模式
  4. Android在程序中动态生成控件,动态布局
  5. android 添加sensor 权限
  6. Android(安卓)Jetpack Compose 之 Text
  7. AndroidManifest 合并出错 tools:replace="android:allowBackup"
  8. Android(安卓)手机端与服务端POST数据交互类
  9. android实现图片平铺效果&WebView多点触控实现缩放

随机推荐

  1. Ubuntu Android开发环境配置
  2. Android自定义属性时TypedArray的使用方
  3. Android(安卓)L新控件RecyclerView简介
  4. Android微件(续)
  5. 详解 Android(安卓)的 Activity 组件
  6. 《Android构建MVVM》系列(一) 之 MVVM架
  7. Android(安卓)应用程序基础
  8. Android实训案例(二)——Android下的CMD
  9. 《Android(安卓)4 高级编程》试读
  10. Android(安卓)属性动画(一):Animator属性动