BroadcastReceiver->服务 - >通知

时间:2014-11-06 12:32:00

标签: android broadcastreceiver android-notifications

我想在BroadcastReceiver(BOOT_COMPLETED)中启动服务并从中进行通知。 像这样:

   public class AutoLoad extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent arg1) {
            Intent i = new Intent(context, MyService.class);
            i.putExtra("screen_state", false);
            context.startService(i);
        }
    }

public class MyService extends Service {

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Notification notification = new         
    NotificationCompat.Builder(getApplicationContext()).setContentTitle("New mail from ").setContentText("Text")
            .setSmallIcon(android.R.drawable.btn_plus).build(); // addAction
        NotificationManager nm = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(0, notification);
        return Service.START_STICKY;
    }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

但我没看到通知。请帮忙!

1 个答案:

答案 0 :(得分:0)

您需要Activity并至少运行才能注册BOOT_COMPLETED意图。