1
我有一個關於從我的應用程序設置每日數據檢查的一般問題(發生某些事件時發出通知)。如何從應用程序設置每天一次的通知
我目前的做法是:
- 從應用程序(的onCreate)我啓動服務
- 從我檢查是否已經有一個匹配的PendingIntent後設置使用AlarmManager報警服務(在這種情況下,我不 需要設置一個新的警報)
- 服務從SharedPreferences檢查數據,並最終顯示的通知
當t爲達到目的,我將鬧鐘設置爲+1分鐘......但這種方式並不奏效。也許我在概念上做錯了什麼?
某些代碼
的AndroidManifest.xml
<application
<activity
android:name=".OverviewActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"
android:configChanges="orientation"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="xxxxx.CheckDaysMonths" >
</service>
</application>
服務CheckDaysMonths
public class CheckDaysMonths extends Service {
private static int ALARM_ID = 131313;
private static String DECLARED_ACTION = "xxxxx.CheckDaysMonths";
@Override
public void onCreate() {
super.onCreate();
PendingIntent alarmIntent = AlarmHelper.getPendingIntentFromAlarm(this, ALARM_ID, DECLARED_ACTION);
if(alarmIntent == null) {
if(someConditionIsMet)
sendNotification(" my notification text ");
// Set an alarm for the next time this service should run:
setAlarm();
}
stopSelf();
}
public void setAlarm() {
AlarmHelper.setAlarm(this, ALARM_ID, DECLARED_ACTION, 8);
}
public void sendNotification(String notifyText) {
Intent mainIntent = new Intent(this, OverviewActivity.class);
@SuppressWarnings("deprecation")
Notification noti = new Notification.Builder(this)
.setAutoCancel(true)
.setContentIntent(PendingIntent.getActivity(this, 131314, mainIntent,
PendingIntent.FLAG_UPDATE_CURRENT))
.setContentTitle("Gratuliere!")
.setContentText("Du bist seit " + notifyText + " rauchfrei!")
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("Du bist seit " + notifyText + " rauchfrei!")
.setWhen(System.currentTimeMillis())
.getNotification();
NotificationManager notificationManager
= (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(131315, noti);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
內部的主要活動(OverviewActivity.java)
編輯:從隱性到顯性
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_overview);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
startService(new Intent(this, CheckDaysMonths.class));
// additional stuff happening on the activity of my app goes here:
...
}
變更服務電話AlarmHelper.java
public class AlarmHelper {
public static PendingIntent getPendingIntentFromAlarm(Context context, int alarmId, String declaredAction) {
return (PendingIntent.getService(context, alarmId,
new Intent(declaredAction),
PendingIntent.FLAG_NO_CREATE));
}
public static void setAlarm(Context context, int alarmId, String declaredAction, int hourOfDay) {
Intent serviceIntent = new Intent(declaredAction);
PendingIntent pi = PendingIntent.getService(context, alarmId, serviceIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
// How long until tomorrow to its hourOfDay?
//DateTime tomorrow = (new DateTime()).plusDays(1);
//DateTime tomorrowAtHourOfDay = new DateTime(tomorrow.getYear(), tomorrow.getMonthOfYear(), tomorrow.getDayOfMonth(), hourOfDay, 0);
DateTime tomorrowAtHourOfDay = (new DateTime()).plusHours(3);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, tomorrowAtHourOfDay.getMillis(), pi);
}
}
你是什麼意思,究竟是「它沒有工作方式」? –
警報似乎沒有起火......我在調試模式下設置了一個斷點,並且在預期的警報超時過期後它沒有進入服務的「onCreate」 – devnull69
嗯,我不知道該建議什麼。你已經省略了一些相關的東西,你服務中的'AlarmHelper.setAlarm()'調用與類中的簽名不匹配,你的描述不太適合代碼等。當你的服務從Activity的'onCreate()'?我不這麼認爲,因爲你使用了一個隱含的Intent,但是清單中的''元素沒有過濾器。此外,這是不允許的,因爲我認爲它是棒棒糖。服務需要以明確的意圖開始;即指定服務類的Intents。 –