这是我第一次尝试制作一个在启动时启动并在后台运行的应用程序,并且我发现所有可能的方法都有点令人困惑。我需要应用程序每隔半小时快速检查几件事情,并在两者之间睡觉,所以我决定使用AlarmManager和setRepeating。
在此测试中,我已将其设置为启动接收器设置警报,该警报应每60秒运行一次服务(实际应用程序在运行此服务之间将超过30分钟)。该服务会触发通知,但是当我重新启动手机时,通知只会显示一次。我清除了它,再也没有看到它。
我将在下面发布相关代码。谁能告诉我哪里出错?谢谢!
AlarmStarter.java
public class AlarmStarter extends BroadcastReceiver {
private static final int PERIOD = 60000;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
scheduleAlarms(context);
}
}
static void scheduleAlarms(Context ctxt) {
AlarmManager mgr = (AlarmManager) ctxt.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(ctxt, BackService.class);
PendingIntent pi = PendingIntent.getService(ctxt, 0, i, 0);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + PERIOD, PERIOD, pi);
}
BackService.java
public class BackService extends Service {
public BackService() {
//showNotification("ticker test", "title test", "content test", "http://google.com");
Log.d("CF", "Starting BackService");
showNotification("ticker test", "title test", "content test", "http://google.com");
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
// Code to execute when the service is first created
}
public void showNotification(String ticker, String title, String content, String url) {
Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this).setTicker(ticker)
.setSmallIcon(android.R.drawable.ic_menu_report_image)
.setContentTitle(title)
.setContentText(content)
.setContentIntent(pi)
.setAutoCancel(true)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testapp.test" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver
android:name=".AlarmStarter"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".BackService"
android:enabled="true"
android:exported="true" ></service>
</application>
</manifest>
答案 0 :(得分:1)
我遇到的问题是我试图通过警报触发的BackService类是从Service类扩展的。它应该从BroadcastReceiver类扩展,因为只有BroadcastReceivers可以由外部发送的警报触发。