【发布时间】:2015-11-14 10:49:20
【问题描述】:
我正在编写一个包含截止日期列表的 Android 应用程序,并且我想在时间到时通知用户其中一个应用程序已在确切的截止日期日期过期,即使该应用程序没有运行也是如此。 我使用了一个 AlarmManager、一个自定义 BroadcastReceiver 和一个自定义 Services 类,但即使我为警报提供了确切的时间,它也总是只有在我运行应用程序时才会触发通知。 这是我设置闹钟的地方:
public static void setAlarm( String string){
AlarmManager alarms =(AlarmManager)Utility.getContext().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(ALARM_INTENT_FILTER);
intent.putExtra("content",string);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Utility.getContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE,00);
calendar.set(Calendar.SECOND,00);
calendar.set(Calendar.MILLISECOND,00);
calendar.set(Calendar.AM_PM,Calendar.PM);
alarms.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
这是广播接收器:
public class NotificationReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Intent service1 = new Intent(context, NotificationServices.class);
service1.putExtra("content",intent.getStringExtra("content"));
context.startService(service1);
}
}
这是服务类:
public class NotificationServices extends Service
{
@Override
public IBinder onBind(Intent arg0) {return null;}
@Override
public void onCreate() {super.onCreate();}
@SuppressWarnings("static-access")
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
JSONObject jsonObject = Utility.jsonObjectFromString(intent.getStringExtra("content"));
DeadLinesList deadlinesList = new DeadLinesList(jsonObject);
Date currentdate = Utility.removeTime(new Date());
String message = "";
for (DeadLine deadline : deadlineList.getDeadline()){
if (deadline.getDateDeadline()!= null && Utility.removeTime(deadline.getDateDeadline()).compareTo(currentdate) == 0){
message += deadline.getDeadlineName()+"\n";
}
}
if (message.length()>0){
NotificationCompat.Builder b = new NotificationCompat.Builder(Utility.getContext());
Intent newIntent = new Intent(Utility.getContext(), MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(Utility.getContext(),0, newIntent, 0);
b.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.drawable.icon)
.setContentTitle("Message")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message))
.setContentIntent(pIntent)
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND).build();
NotificationManager notificationManager = (NotificationManager) Utility.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, b.build());
}
return START_NOT_STICKY;
}
@Override
public void onDestroy() {super.onDestroy();}
}
这是我注册服务和接收者的清单:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.android.alarm.permission.WAKE_LOCK"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:largeHeap="true">
<receiver android:name=".Notifications.NotificationReceiver">
<intent-filter>
<action android:name="ALARM_ACTION"/>
</intent-filter>
</receiver>
<service android:name=".Notifications.NotificationServices"
android:enabled="true">
</service>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="adjustPan"
android:configChanges="keyboardHidden|orientation|screenSize">
<meta-data
android:name="android.app.default_searchable"
android:value=".MainActivity" />
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
错误在哪里?为什么通知仅在我打开应用时触发,而不是在一天中的确切时间或应用未运行时触发?
【问题讨论】:
标签: android service notifications broadcastreceiver alarmmanager