Jay*_*dri 2 service android broadcastreceiver android-pendingintent repeatingalarm
我有以下广播接收器,当手机启动时被调用(Bootreciever.java)并且我正在尝试使用重复警报启动间歇运行的服务.
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"Inside onRecieve() :D" , Toast.LENGTH_LONG).show();
Log.d(getClass().getName(), "Inside onRecieve() :D");
AlarmManager am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, MyService.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + 3000, 1000, pi);
}
Run Code Online (Sandbox Code Playgroud)
基本上,我设置一个重复警报触发3秒,之后每秒触发一次.收到启动完成广播就好 - 但服务没有启动.MyService.java看起来像这样:
public class MyService extends Service {
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Hello from MyService!", Toast.LENGTH_LONG);
Log.d(getClass().getName(), "Hello from MyService!");
stopSelf();
return super.onStartCommand(intent, flags, startId);
}
Run Code Online (Sandbox Code Playgroud)
启动服务时我做错了什么?
Logcat没有告诉我任何事情,我在清单中定义了服务.这个服务本身在调用时工作,startService()但在a中使用时似乎失败了PendingIntent.
而不是PendingIntent.getBroadcast()使用PendingIntent.getService()