设置定时状态栏通知

Set timed status bar notification

我想在这个例子的特定时间按下按钮时激活通知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 的 class,其中包含以下代码:

@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.Notify(MainActivity.java:61)
09-27 12:55:57.836: E/AndroidRuntime(18715):    at com.example.notify.MainActivity.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)

我的最终目标是为明天、下周和下个月设置通知,如果您知道如何使用未弃用的方法,我将非常感激知道,我也没有任何如果需要,清单中的接收器或实现请告诉我。

尝试发出这样的通知:

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); 

下面是带有按钮点击事件的activity。它将在特定时间设置闹钟。它将通知广播接收器。

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>

定义广播接收器class:

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); 
        }
    }
}