如何才能获得每5分钟一次的通知,用户必须从android中的通知栏启动该通知?

时间:2016-04-19 11:48:53

标签: android notifications alarmmanager android-broadcastreceiver

我是Android的新手,我正在使用警报管理器和接收器以及通知开发每天通知的应用程序。但现在我面临两个问题

  1. 如果设备已关闭,我没有收到任何通知(我认为它会永久终止)并且

  2. 我需要每5分钟显示一次通知,直到用户使用通知栏中的通知启动应用,但我不确定如何。

    public class AlarmReciever extends WakefulBroadcastReceiver {
    
      AlarmManager alarmManager;
      PendingIntent pendingIntent;
      int RequestCode =777;
    
      DBHelper helper;
      SQLiteDatabase database;
      SQLiteStatement statement;
      Cursor cursor;
      int datacount=0;
    
      public void onReceive(Context context, Intent intent) {
          String rec = intent.getDataString();
          NotifyMessage(context);
      }
    
      public void NotifyMessage(Context context){
          Intent intent = new Intent(context, NotificationTabs.class);
          intent.putExtra("Medicine", "Medicine");
          PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    
        NotificationCompat.Builder builder = new NotificationCompat.Builder(
            context)
        .setSmallIcon(R.drawable.ic_launcher)
        .setTicker("Take medicine")
        .setContentTitle(context.getString(R.string.notificationtitle))
        .setContentText("Take medicine")
        .addAction(R.drawable.ic_launcher, "Action Button", pIntent)
        .setContentIntent(pIntent)
        .setAutoCancel(true);
    
    
          NotificationManager notificationmanager = (NotificationManager) context
        .getSystemService(Context.NOTIFICATION_SERVICE);
    
        notificationmanager.notify(0, builder.build());
    
    }
    
  3. 我能否就如何解决这些问题寻求帮助?

1 个答案:

答案 0 :(得分:0)

我为你的第二个问题做了一个小概念验证。请在你的活动中添加以下代码:

Intent notificationIntent=new Intent(context,DisplayNotification.class);
        PendingIntent contentIntent=PendingIntent.getService(context,0,notificationIntent,PendingIntent.FLAG_CANCEL_CURRENT);

        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        am.cancel(contentIntent);
        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),( AlarmManager.INTERVAL_FIFTEEN_MINUTES)/3, contentIntent);

这是我的DisplayNotification服务类:

public class DisplayNotification extends Service {
    private final static String TAG = "ShowNotification";

    @Override
    public void onCreate() {
        super.onCreate();
        Intent mainIntent=new Intent(this,MainActivity.class);
        NotificationManager notificationManager= (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

        Notification noti=new NotificationCompat.Builder(this)
                .setAutoCancel(true)
                .setContentIntent(PendingIntent.getActivity(this,0,mainIntent,PendingIntent.FLAG_UPDATE_CURRENT))
                .setContentTitle("Test Notification " + System.currentTimeMillis())
                .setContentText("Cricket news Dhoni is bold")
                .setDefaults(Notification.DEFAULT_ALL)
                .setSmallIcon(R.drawable.bell)
                .setTicker("match update")
                .setWhen(System.currentTimeMillis())
                .build();
        notificationManager.notify(0, noti);

        Log.i(TAG, "Notification created");
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

接收器类代码:

public class MyBroadcastReceiver extends BroadcastReceiver {  
    MediaPlayer mp;  
    @Override  
    public void onReceive(Context context, Intent intent) {  
        mp=MediaPlayer.create(context, R.raw.alrm   );  
        mp.start();  
        Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();  
    }  
}

请在清单中声明接收方:

  <uses-permission android:name="android.permission.VIBRATE" /> 


    <receiver android:name="MyBroadcastReceiver" >  
            </receiver>