我正在创建提醒通知。我使用AlarmService类(我在其中创建通知)和一个扩展BroadcastReceiver的AlarmReceiver类。通知已正确创建,但我仅在特定时间段内收到通知。例如,如果我在30分钟后设置提醒,则通知不起作用。在我的服务类中,我返回START_STICKY。这是我的代码:
public class AlarmService extends Service{
....
while(...){
//call to the method to create notifications
createNotification(year, month, day_of_month, hour_of_day, minute, second, reminderID);
}
return START_STICKY;
}
//method to create notifications
public void createNotification(int year, int month, int day_of_month, int hour_of_day, int
minute, int second, int reminderID){
counter++;
Calendar cal = Calendar.getInstance();
// add minutes to the calendar object
cal.set(Calendar.MONTH,month); // the month starts with JAN == 0
cal.set(Calendar.YEAR,year);
cal.set(Calendar.DAY_OF_MONTH,day_of_month);
cal.set(Calendar.HOUR_OF_DAY,hour_of_day);
cal.set(Calendar.MINUTE,minute);
cal.set(Calendar.SECOND,second);
Intent alarmintent = new Intent(getApplicationContext(), AlarmReceiver.class);
.......
PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), HELLO_ID+counter, alarmintent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
}
/////////////////////////////////////////////// ///////////////////////////////
public class AlarmReceiver extends BroadcastReceiver {
private static int NOTIFICATION_ID = 555;
@Override
public void onReceive(Context context, Intent intent) {
........
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context)
.setSmallIcon(R.drawable.android100)
.setContentTitle(note)
.setLargeIcon(bitmap)
.setContentText(title)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
Intent notifyIntent = new Intent(context, DisplayReminder.class);
notifyIntent.putExtra("reminderID", ""+reminderID);
PendingIntent contentIntent = PendingIntent.getActivity(context, reminderID, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(contentIntent);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID++, mBuilder.build());
}.....
最后,我以这种方式启动服务表单Activity(不是主要活动):
Intent alarmServiceIntent = new Intent(getApplicationContext(), AlarmService.class);
startService(alarmServiceIntent);
这是我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.papp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="14" />
<application
android:allowBackup="true"
android:icon="@drawable/android100"
android:label="@string/app_name"
android:theme="@style/AppHoloTheme">
<activity
android:name=".MainActivity"
android:label="Le tue medicine"
android:screenOrientation="landscape">
</activity>
<activity
android:label="Inserisci un nuovo promemoria"
android:name=".InsertNewReminder">
</activity>
<activity
android:label="Anteprima promemoria"
android:name=".DisplayOverview">
</activity>
<receiver android:name=".AlarmReceiver">
<!-- <intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter> -->
</receiver>
<service android:name=".AlarmService"></service>
<activity
android:label="@string/app_name"
android:name=".GalleryListMainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="Salvataggio promemoria"
android:name="com.example.papp.DbActivity">
</activity>
<activity
android:label="Salvataggio promemoria"
android:name=".InsertNewRemindeResponse"
android:launchMode="singleTask">
</activity>
<activity
android:label="Promemoria"
android:name=".DisplayReminder">
</activity>
<activity
android:label="Modifica promemoria"
android:name=".UpdateReminder">
</activity>
<receiver android:name=".BootReceiver" android:enabled="true" android:exported="true">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<activity
android:label="Cancellazione promemoria"
android:name=".DeleteReminder"
android:launchMode="singleTask">
</activity>
</application>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
</manifest>
我尝试了不同的尝试,但没有成功:我确信如果我手动终止应用程序,服务会自动重启(感谢START_STICKY)。 有什么建议?感谢...