Android 广播接收器->服务->通知
我想在BroadcastReceiver启动时启动服务,并从中发出通知。 像这样:Android 广播接收器->服务->通知,android,broadcastreceiver,android-notifications,Android,Broadcastreceiver,Android Notifications,我想在BroadcastReceiver启动时启动服务,并从中发出通知。 像这样: public class AutoLoad extends BroadcastReceiver { @Override public void onReceive(Context context, Intent arg1) { Intent i = new Intent(context, MyService.class); i.
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;
}
但我看不到任何通知。请帮忙 您需要一个活动并至少运行一次,以便能够注册以启动已完成的意图。您不需要服务来发出通知-您现有的代码可以直接进入onReceive。你还泄露了你的服务,这将严重激怒用户。除此之外,您是否有一项活动,并且是否已运行该活动?否则,您的清单注册接收者将无法工作。为什么要这样做,但您可以从onReceive显示通知消息。你能解释一下你为什么需要这个吗。我想做后台网络,这就是为什么我使用这个服务。这是对的吗?真的吗?我不能在不启动活动的情况下通过BroadcastReceiver启动服务吗?