80,349
社区成员
发帖
与我相关
我的任务
分享
public class UpdateService extends IntentService {
public UpdateService() {
super("UpdateService");
System.out.println( "开机自动服务自动启动.....");
}
@Override
protected void onHandleIntent(Intent intent) {
getNotification();
}
@Override
public void onDestroy(){
super.onDestroy();
Log.d("Demo","onDestroy 已执行");
}
public void getNotification() {
NotificationManager manager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
Notification notification = new Notification();
notification.tickerText = "来消息了";
notification.icon = R.mipmap.image2;
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.defaults =Notification.DEFAULT_VIBRATE;
long when = System.currentTimeMillis();
notification.when = when;
Intent intent = new Intent(getApplication(), FoodDetailed.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, Intent.FILL_IN_ACTION);
Notification.Builder builder = new Notification.Builder(this);
builder.setContentTitle("Bmob Test");
builder.setContentText("IM start up");
builder.setSmallIcon(R.drawable.img1);
//Notification builder = new Notification.Builder(this).setTicker("显示于屏幕顶端状态栏的文本").setSmallIcon(R.mipmap .image2).build();
manager.notify(0, notification);
}
}
public class DeviceStartedListener extends BroadcastReceiver {
public DeviceStartedListener() {
}
@Override
public void onReceive(Context context, Intent intent) {
Log.e("TAG", "开机自动服务自动启动.....");
//后边的XXX.class就是要启动的服务
Intent serviceIntent = new Intent(context, UpdateService.class);
context.startService(serviceIntent);
System.out.println( "开机自动服务自动启动.....");
}
}