我想激活一個通知,當我按下一個按鈕,在某一時刻在這個例子中12:56所以我在我的代碼中使用這樣的:設置定時狀態欄通知
Button notificationButton = (Button) findViewById(R.id.button1);
notificationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this , Notify("Title: Meeting with Business",
"Msg:Pittsburg 10:00 AM EST "));
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent1 = PendingIntent.getService(MainActivity.this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 56);
calendar.set(Calendar.SECOND, 00);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pendingIntent1);
,我創建了一個名爲類通知包含這些代碼:在運行應用程序後
@SuppressWarnings("deprecation")
private Class<?> Notify(String notificationTitle, String notificationMessage) {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
@SuppressWarnings("deprecation")
Notification notification = new Notification(R.drawable.ic_launcher,
"New Message", System.currentTimeMillis());
notification.setLatestEventInfo(getBaseContext(), notificationTitle, notificationMessage, null);
notificationManager.notify();
return null;
}
我得到這個錯誤:
09-27 12:55:57.836: E/AndroidRuntime(18715): FATAL EXCEPTION: main
09-27 12:55:57.836: E/AndroidRuntime(18715): Process: com.example.notify, PID: 18715
09-27 12:55:57.836: E/AndroidRuntime(18715): java.lang.IllegalMonitorStateException: object not locked by thread before notify()
09-27 12:55:57.836: E/AndroidRuntime(18715): at java.lang.Object.notify(Native Method)
09-27 12:55:57.836: E/AndroidRuntime(18715): at com.example.notify.MainActivity$1.Notify(MainActivity.java:61)
09-27 12:55:57.836: E/AndroidRuntime(18715): at com.example.notify.MainActivity$1.onClick(MainActivity.java:35)
09-27 12:55:57.836: E/AndroidRuntime(18715): at android.view.View.performClick(View.java:5184)
09-27 12:55:57.836: E/AndroidRuntime(18715): at android.view.View$PerformClick.run(View.java:20910)
09-27 12:55:57.836: E/AndroidRuntime(18715): at android.os.Handler.handleCallback(Handler.java:739)
09-27 12:55:57.836: E/AndroidRuntime(18715): at android.os.Handler.dispatchMessage(Handler.java:95)
09-27 12:55:57.836: E/AndroidRuntime(18715): at android.os.Looper.loop(Looper.java:145)
09-27 12:55:57.836: E/AndroidRuntime(18715): at android.app.ActivityThread.main(ActivityThread.java:5942)
09-27 12:55:57.836: E/AndroidRuntime(18715): at java.lang.reflect.Method.invoke(Native Method)
09-27 12:55:57.836: E/AndroidRuntime(18715): at java.lang.reflect.Method.invoke(Method.java:372)
09-27 12:55:57.836: E/AndroidRuntime(18715): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
09-27 12:55:57.836: E/AndroidRuntime(18715): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
我最終的G oal將爲明天,下週和下個月設置通知,並且如果您知道如何使用未被棄用的方法,我將非常感激知道,如果存在清單,我也沒有任何接收器或清單中的實現是需要這樣告訴我的。
泰,有一些errror的一個是字符串title = intent.getExtra(「Title」);應該是intent .getstringextras(「title」) – Soheyl
另一個錯誤是:Intent intent1 = new Intent(this,MainActivity.class);這給我錯誤.i爲wakupreciever創建一個單獨的類文件。 – Soheyl
並且這行說counstructor沒有定義:Notification n = new Notification.Builder(this) – Soheyl