所以我不知道爲什麼這段代碼不工作。我想發出一個「警報」通知,每天一次。只是想說,即時通訊新的android。謝謝。代碼無法正常工作。通知應該每天關閉一次
編輯:改變了一下代碼。報警方法執行和通知也一樣,但我得到這個錯誤信息:
-248 /? D/PowerManagerService:releaseWakeLock標誌= 0x1 tag = AlarmManager W/ActivityManager:無法啓動服務意圖{flg = 0x4 cmp = com.example.polakken.test/.lol(有額外)}:未找到06-13 00:00 :00.825 231-267 /? D/PowerManagerService:acquireWakeLock標誌= 0x1標籤= AlarmManager 06-13 00:00:00.825 231-248 /? d/PowerManagerService:releaseWakeLock標誌=爲0x1 =標籤AlarmManager -
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
int i = preferences.getInt("numberoflaunches", 1);
if (i < 2) {
alarmMethod();
i++;
editor.putInt("numberoflaunches", i);
editor.commit();
}
if (savedInstanceState == null) {
splashMethod();
}
}
//...
private void alarmMethod() {
Intent intentbro = new Intent(this, lol.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intentbro, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
calendar.add(Calendar.DAY_OF_MONTH, 1);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 60 * 24, pendingIntent);
Toast.makeText(MainActivity.this, "start alarm", Toast.LENGTH_LONG).show();
}
//notification class
public class lol extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
NotificationCompat.Builder b = new NotificationCompat.Builder(this);
Intent intent1 = new Intent(this.getApplicationContext(), MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 1, intent1, 0);
b.setContentText("lol");
b.setContentTitle("Default notification");
b.setSmallIcon(R.drawable.iconography_small_size);
b.setContentIntent(pIntent);
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, b.build());
}
}
如果發生什麼事情,你究竟發生了什麼?你有錯誤嗎?如果是這樣,發佈它們。 – JNYRanger 2015-02-23 19:46:44
@JNYRanger問題是沒有錯誤的。應用程序運行完美但無法按代碼中列出的時間顯示通知 – 2015-02-23 19:53:28
您應該測試即將到來的日曆時間,並在代碼中設置斷點。查看您的斷點是否執行,並告訴我們您的代碼的哪一部分未按預期工作。 – 2015-02-23 19:57:41