我有一个BroadcastReceiver,它包含以下代码:
package de.my.app;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent alarmIntent = new Intent(context,AlarmService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
int interval = 10000;
manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Log.e("RECEIVER", "WORKS");
}
}
在我的日志中,Receiver的日志消息显示,但来自服务的消息未显示。 如果它能正常工作,它会每10秒显示一次。 这是我的AlarmService类:
package de.my.app;
import android.app.IntentService;
import android.app.NotificationManager;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class AlarmService extends IntentService {
public AlarmService(
) {
super("AlarmService");
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
protected void onHandleIntent(Intent intent) {
Log.e("SERVICE","WORKS");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY_COMPATIBILITY;
}
@Override
public void onDestroy() {
super.onDestroy();
sendBroadcast(new Intent("restart"));
}
}
我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.my.app">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<permission
android:name="de.my.app.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="de.my.app.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MyActivity"
android:label="@string/app_name"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SettingsActivity"
android:label="Einstellungen">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="de.my.app.MyActivity" />
</activity>
<activity
android:name=".Start"
android:label="@string/app_name" />
<receiver
android:name=".GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.hmkcode.android.gcm" />
</intent-filter>
</receiver>
<service android:name=".GcmIntentService" />
<receiver
android:name=".BootReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="restart" />
</intent-filter>
</receiver>
<service android:name=".AlarmService" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
答案 0 :(得分:2)
而不是在以下行中使用getBroadcast(...)
...
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
...您应该使用getService(...)
。
简单地说 - 如果您希望将PendingIntent
用于Activity
,Service
或Broadcast
,则需要使用正确的方法,具体取决于哪种类型它的应用程序组件。
...试
PendingIntent pendingIntent = PendingIntent.getService(context, 0, alarmIntent, 0);
答案 1 :(得分:0)
我这样做
//class ScheduledServiceDemoActivity
public class ScheduledServiceDemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PollReceiver.scheduleAlarms(this);
Toast.makeText(this, R.string.alarms_scheduled, Toast.LENGTH_LONG)
.show();
finish();
}
}
//class PollReceiver
public class PollReceiver extends BroadcastReceiver {
private static final int PERIOD=30000; // 30sec
private static final int INITIAL_DELAY=5000; // 5 seconds
@Override
public void onReceive(Context ctxt, Intent i) {
if (i.getAction() == null) {
WakefulIntentService.sendWakefulWork(ctxt, ScheduledService.class);
}
else {
scheduleAlarms(ctxt);
}
}
static void scheduleAlarms(Context ctxt) {
AlarmManager mgr=
(AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
Intent i=new Intent(ctxt, PollReceiver.class);
PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0, i, 0);
mgr.setRepeating(AlarmManager.RTC_WAKEUP,
SystemClock.elapsedRealtime() + INITIAL_DELAY,
PERIOD, pi);
}
}
//class ScheduledService
public class ScheduledService extends WakefulIntentService {
public ScheduledService() {
super("ScheduledService");
}
@Override
protected void doWakefulWork(Intent intent) {
Log.d(getClass().getSimpleName(), "I ran!"); // will run after 30 sec
}
}
// mainfest
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:name="ScheduledServiceDemoActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver android:name="PollReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<service android:name="ScheduledService">
</service>
</application>