2015-09-27 63 views
0

我想激活一个通知,当我按下一个按钮,在某一时刻在这个例子中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将为明天,下周和下个月设置通知,并且如果您知道如何使用未被弃用的方法,我将非常感激知道,如果存在清单,我也没有任何接收器或清单中的实现是需要这样告诉我的。

回答

1

以下是按钮clic k事件。它会在特定的时间设置闹钟。它会通知广播接收机。

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文件添加此意图过滤和注册接收器:

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

泰,有一些errror的一个是字符串title = intent.getExtra(“Title”);应该是intent .getstringextras(“title”) – Soheyl

+0

另一个错误是:Intent intent1 = new Intent(this,MainActivity.class);这给我错误.i为wakupreciever创建一个单独的类文件。 – Soheyl

+0

并且这行说counstructor没有定义:Notification n = new Notification.Builder(this) – Soheyl

-1

你可以叫错法(这是Object.notify()):

notificationManager.notify(); 

NotificationManager需要notify方法:

public void notify (int id, Notification notification); 
public void notify (String tag, int id, Notification notification); 
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);