我想在按下按钮时激活通知,在本例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);
我创建了一个名为Notify的类,其中包含以下代码:
@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)
我的最终目标是为明天,下周和下个月设置通知,如果您知道如何使用未弃用的方法,我将非常感谢知道,我也没有任何接收器或如果需要,请在清单中实现告诉我。
答案 0 :(得分:1)
以下是按钮点击事件的活动。它会在特定时间设置闹钟。它将通知广播接收者。
Button notificationButton = (Button) findViewById(R.id.button1);
notificationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this , WakeUpReceiver.class);
myIntent.setAction("com.intent.action.MEETING_BUSINESS");
myIntent.putExtra("Title","Meeting with Business");
myIntent.putExtra("Message","Pittsburg 10:00 AM EST");
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(YourActivityName.this, 0, myIntent , PendingIntent.FLAG_CANCEL_CURRENT);
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 , pendingIntent);
}
}
广播接收者实际上负责解雇通知。
在Android manifest.xml文件中添加此intent过滤器并注册接收器:
<receiver android:name="com.package.WakeUpReceiver">
<intent-filter>
<action android:name="com.intent.action.MEETING_BUSINESS" />
</intent-filter>
</receiver>
定义广播接收器类:
packager com.package;
public class WakeUpReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("com.intent.action.MEETING_BUSINESS")){
String title = intent.getStringExtra("Title");
String message = intnet.getStringExtra("Message");
// if you want to open the application when click on notification
// then pass the activity name here.
PendingIntent intent1 = PendingIntent.getActivity(context,0, intent, 0); //<- pass your activity name here.
Notification n = new Notification.Builder(context)//<-- pass the context
.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.drawable.icon)
.setContentIntent(intent1)
.setAutoCancel(true).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, n);
}
}
}
答案 1 :(得分:0)
尝试发出如下通知:
Intent notIntent = new Intent(this, Receiver.class);
PendingIntent pendIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), notIntent, 0);
Notification n = new Notification.Builder(this)
.setContentTitle("Contenttitle")
.setContentText("Contenttext")
.setSmallIcon(R.drawable.smallicon)
.setContentIntent(pendIntent)
.setAutoCancel(true)
.addAction(R.drawable.smallicon, "Call", pendIntent).build();
NotificationManager notManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notManager.notify(0, n);