2017-08-18 90 views
0

我正在卸載一個應用程序android.I有AlarmManager.This AlarmManager不能stoppable.Everytime(每2秒)根據隨機數檢查我的列表。隨機數檢查數據庫並給予決定NotificationManager.İf列表行是不green.It使it.My問題是,當行green.My通知文本沒有按「餘噸change.How可以修復它如何更改通知文本?

我AlarmManagerReceiver:

DatabaseHelper mDatabaseHelper; 
    ArrayList listData = new ArrayList<>(); 
    ArrayList listDataoran = new ArrayList<>(); 
    ArrayList listDataBoya = new ArrayList<>(); 
    ArrayList listDataId = new ArrayList<>(); 
    ArrayList listDatanumber = new ArrayList<>(); 
Random secilmiskisi ; 
int secilmissayı; 


    @Override 
    public void onReceive(Context context, Intent intent) { 
     mDatabaseHelper = new DatabaseHelper(context); 
     Cursor data = mDatabaseHelper.getData(); 
     while(data.moveToNext()){ 

      listDataId.add(data.getString(0)); 

      listData.add(data.getString(1)); 


      listDatanumber.add(data.getString(2)); 

      listDataoran.add(data.getString(3)); 
      listDataBoya.add(data.getString(4));} 

secilmiskisi = new Random(); 
     secilmissayı = secilmiskisi.nextInt(listDataId.size()) + 0; 

     Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); 
     cancelAlarm(context); 

    // setalarm(context); 


if(!listDataBoya.contains("#1eac02")){ 



    String table = "people_table"; 
     ContentValues productDetailsContentValues = new ContentValues(); 
       productDetailsContentValues.put("boya", "#1eac02"); 
       String where = " id = " + listDataId.get(secilmissayı); 
       mDatabaseHelper.update(table, productDetailsContentValues, where , null); 



     Intent yesReceive = new Intent(); 
     yesReceive.setAction(AppConstant.YES_ACTION); 
     PendingIntent pendingIntentYes = PendingIntent.getBroadcast(context, 12345, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT); 
Intent intenttt = new Intent(context,AnaMenu.class); 
     PendingIntent pIntent = PendingIntent.getActivity(context, 0, intenttt, 0); 


     Intent yesReceive2 = new Intent(); 
     yesReceive2.setAction(AppConstant.STOP_ACTION); 
     PendingIntent pendingIntentYes2 = PendingIntent.getBroadcast(context, 12345, yesReceive2, PendingIntent.FLAG_UPDATE_CURRENT); 
     // Build notification 
     // Actions are just fake 
     Notification noti = null; 
     if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { 
      noti = new Notification.Builder(context) 
        .setTicker(" size bir bildirim yolladı.") 
        .setContentTitle("") 
        .setContentText(listData.get(secilmissayı)+" i arama zamanı") 
        .setSmallIcon(R.drawable.familyiiii) 

        .addAction(R.drawable.cal, "Ara", pendingIntentYes) 
        .addAction(R.drawable.se, "Daha Sonra", pendingIntentYes2) 

        .setContentIntent(pIntent).getNotification(); 
     } 
     // .setTicker(" size bir bildirim yolladı.").setSmallIcon(R.drawable.familyiiii) 
     //  .setSubText("Bu mesaj ile artık" + isimler.get(random) + " arayacağın bir görevin oldu.") 
     NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); 
     noti.flags |= Notification.FLAG_AUTO_CANCEL; 
     notificationManager.notify(0, noti); 
     setalarm(context); 

}else if (listDataBoya.get(secilmissayı) != "#1eac02" ){ 

String table = "people_table"; 
     ContentValues productDetailsContentValues = new ContentValues(); 
       productDetailsContentValues.put("boya", "#1eac02"); 
      String where = " id = " + listDataId.get(secilmissayı); 
      mDatabaseHelper.update(table, productDetailsContentValues, where , null); 


     Intent yesReceive = new Intent(); 
     yesReceive.setAction(AppConstant.YES_ACTION); 
     PendingIntent pendingIntentYes = PendingIntent.getBroadcast(context, 12345, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT); 


Intent intenttt = new Intent(context, AnaMenu.class); 
     PendingIntent pIntent = PendingIntent.getActivity(context, 0, intenttt, 0); 


     Intent yesReceive2 = new Intent(); 
     yesReceive2.setAction(AppConstant.STOP_ACTION); 
     PendingIntent pendingIntentYes2 = PendingIntent.getBroadcast(context, 12345, yesReceive2, PendingIntent.FLAG_UPDATE_CURRENT); 
     // Build notification 
     // Actions are just fake 
     Notification noti = null; 
     if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { 
      noti = new Notification.Builder(context) 
        .setTicker(" size bir bildirim yolladı.") 
        .setContentTitle("") 
       .setContentText(listData.get(secilmissayı)+" i arama zamanı") 
        .setSmallIcon(R.drawable.familyiiii) 

        .addAction(R.drawable.cal, "Ara", pendingIntentYes) 
        .addAction(R.drawable.se, "Daha Sonra", pendingIntentYes2) 

        .setContentIntent(pIntent).getNotification(); 
     } 
     // .setTicker(" size bir bildirim yolladı.").setSmallIcon(R.drawable.familyiiii) 
     //  .setSubText("Bu mesaj ile artık" + isimler.get(random) + " arayacağın bir görevin oldu.") 
     NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); 
     noti.flags |= Notification.FLAG_AUTO_CANCEL; 
     notificationManager.notify(0, noti); 
setalarm(context); 

}while (listDataBoya.get(secilmissayı) == "#1eac02"){////Here , is the problem. 
String table = "people_table"; 
     ContentValues productDetailsContentValues = new ContentValues(); 
       productDetailsContentValues.put("boya", "#1eac02"); 
      String where = " id = " + listDataId.get(secilmissayı); 
      mDatabaseHelper.update(table, productDetailsContentValues, where , null); 


     Intent yesReceive = new Intent(); 
     yesReceive.setAction(AppConstant.YES_ACTION); 
     PendingIntent pendingIntentYes = PendingIntent.getBroadcast(context, 12345, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT); 


Intent intenttt = new Intent(context, AnaMenu.class); 
     PendingIntent pIntent = PendingIntent.getActivity(context, 0, intenttt, 0); 


     Intent yesReceive2 = new Intent(); 
     yesReceive2.setAction(AppConstant.STOP_ACTION); 
     PendingIntent pendingIntentYes2 = PendingIntent.getBroadcast(context, 12345, yesReceive2, PendingIntent.FLAG_UPDATE_CURRENT); 
     // Build notification 
     // Actions are just fake 
     Notification noti = null; 
     if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { 
      noti = new Notification.Builder(context) 
        .setTicker("AYNIKİŞİAYNIKİŞİAYNIKİŞİAYNIKİŞİAYNIKİŞİAYNIKİŞİ") 
        .setContentTitle("AYNIKİŞİAYNIKİŞİAYNIKİŞİAYNIKİŞİAYNIKİŞİ") 
       .setContentText(listData.get(secilmissayı)+" i arama zamanı") 
        .setSmallIcon(R.drawable.familyiiii) 

        .addAction(R.drawable.cal, "Ara", pendingIntentYes) 
        .addAction(R.drawable.se, "Daha Sonra", pendingIntentYes2) 

        .setContentIntent(pIntent).getNotification(); 
     } 
     // .setTicker(" size bir bildirim yolladı.").setSmallIcon(R.drawable.familyiiii) 
     //  .setSubText("Bu mesaj ile artık" + isimler.get(random) + " arayacağın bir görevin oldu.") 
     NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); 
     noti.flags |= Notification.FLAG_AUTO_CANCEL; 
     notificationManager.notify(0, noti); 
setalarm(context); 




} 

這是第一次活動有創建:enter image description here

Thi s是最後一次活動:enter image description here

我想在選定號碼的行(隨機數)爲綠色時更改通知文本。

+0

您是否閱讀過有關更改通知的文檔https://developer.android.com/training/notify-user/managing.html? – Adinia

+0

[Update notification of notification,not entire notification]可能重複(https://stackoverflow.com/questions/14885368/update-text-of-notification-not-entire-notification) – Adinia

回答

0
String subtext[]={"text one","text two","text three","text four"......}; 


    int ran=new Random().nextInt(4); 

android.app.Notification notif = new android.app.Notification.Builder(getApplicationContext()) 
 
       .setContentTitle("M-Educate welcomes you the Digital era of learning") 
 
       .setSubText(subtext[ran]) 
 
       .setSmallIcon(R.drawable.icn_app_icon) 
 
       .setLargeIcon(icon) 
 
       .setContentIntent(pendingIntent) 
 
       .setSound(alarmSound) 
 
       .build(); 
 
     NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); 
 
     notificationManager.notify(0, notif);

注:設置你的文本作爲一個數組中,並在每次射擊的通知

+0

它沒有工作。我的問題是當while循環沒有工作,當線變綠。它沒有顯示文本.. –

0

時間設置隨機文本試試這個代碼,替換現有的一個:

DatabaseHelper mDatabaseHelper; 
     ArrayList listData = new ArrayList<>(); 
     ArrayList listDataoran = new ArrayList<>(); 
     ArrayList listDataBoya = new ArrayList<>(); 
     ArrayList listDataId = new ArrayList<>(); 
     ArrayList listDatanumber = new ArrayList<>(); 
     Random secilmiskisi ; 
     int secilmissayı; 


@Override 
public void onReceive(Context context, Intent intent) { 
     mDatabaseHelper = new DatabaseHelper(context); 
     Cursor data = mDatabaseHelper.getData(); 
     while(data.moveToNext()){ 

     listDataId.add(data.getString(0)); 

     listData.add(data.getString(1)); 


     listDatanumber.add(data.getString(2)); 

     listDataoran.add(data.getString(3)); 
     listDataBoya.add(data.getString(4));} 

//  secilmiskisi = new Random(); 
//  secilmissayı = secilmiskisi.nextInt(listDataId.size()) + 0; 
//  As you have declare the random number globaly it wont cahnge 

     Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); 
     cancelAlarm(context); 

     // setalarm(context); 


     if(!listDataBoya.contains("#1eac02")){ 

     secilmiskisi = new Random(); 
     secilmissayı = secilmiskisi.nextInt(listDataId.size()) + 0; 
//  Place the random thing in local 

     String table = "people_table"; 
     ContentValues productDetailsContentValues = new ContentValues(); 
     productDetailsContentValues.put("boya", "#1eac02"); 
     String where = " id = " + listDataId.get(secilmissayı); 
     mDatabaseHelper.update(table, productDetailsContentValues, where , null); 



     Intent yesReceive = new Intent(); 
     yesReceive.setAction(AppConstant.YES_ACTION); 
     PendingIntent pendingIntentYes = PendingIntent.getBroadcast(context, 12345, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT); 
     Intent intenttt = new Intent(context,AnaMenu.class); 
     PendingIntent pIntent = PendingIntent.getActivity(context, 0, intenttt, 0); 


     Intent yesReceive2 = new Intent(); 
     yesReceive2.setAction(AppConstant.STOP_ACTION); 
     PendingIntent pendingIntentYes2 = PendingIntent.getBroadcast(context, 12345, yesReceive2, PendingIntent.FLAG_UPDATE_CURRENT); 
     // Build notification 
     // Actions are just fake 
     Notification noti = null; 
     if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { 
     noti = new Notification.Builder(context) 
     .setTicker(" size bir bildirim yolladı.") 
     .setContentTitle("") 
     .setContentText(listData.get(secilmissayı)+" i arama zamanı") 
     .setSmallIcon(R.drawable.familyiiii) 

     .addAction(R.drawable.cal, "Ara", pendingIntentYes) 
     .addAction(R.drawable.se, "Daha Sonra", pendingIntentYes2) 

     .setContentIntent(pIntent).getNotification(); 
     } 
     // .setTicker(" size bir bildirim yolladı.").setSmallIcon(R.drawable.familyiiii) 
     //  .setSubText("Bu mesaj ile artık" + isimler.get(random) + " arayacağın bir görevin oldu.") 
     NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); 
     noti.flags |= Notification.FLAG_AUTO_CANCEL; 
     notificationManager.notify(0, noti); 
     setalarm(context); 

     }else if (listDataBoya.get(secilmissayı) != "#1eac02" ){ 
     secilmiskisi = new Random(); 
     secilmissayı = secilmiskisi.nextInt(listDataId.size()) + 0; 
//and here also 
     String table = "people_table"; 
     ContentValues productDetailsContentValues = new ContentValues(); 
     productDetailsContentValues.put("boya", "#1eac02"); 
     String where = " id = " + listDataId.get(secilmissayı); 
     mDatabaseHelper.update(table, productDetailsContentValues, where , null); 


     Intent yesReceive = new Intent(); 
     yesReceive.setAction(AppConstant.YES_ACTION); 
     PendingIntent pendingIntentYes = PendingIntent.getBroadcast(context, 12345, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT); 


     Intent intenttt = new Intent(context, AnaMenu.class); 
     PendingIntent pIntent = PendingIntent.getActivity(context, 0, intenttt, 0); 


     Intent yesReceive2 = new Intent(); 
     yesReceive2.setAction(AppConstant.STOP_ACTION); 
     PendingIntent pendingIntentYes2 = PendingIntent.getBroadcast(context, 12345, yesReceive2, PendingIntent.FLAG_UPDATE_CURRENT); 
     // Build notification 
     // Actions are just fake 
     Notification noti = null; 
     if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { 
     noti = new Notification.Builder(context) 
     .setTicker(" size bir bildirim yolladı.") 
     .setContentTitle("") 
     .setContentText(listData.get(secilmissayı)+" i arama zamanı") 
     .setSmallIcon(R.drawable.familyiiii) 

     .addAction(R.drawable.cal, "Ara", pendingIntentYes) 
     .addAction(R.drawable.se, "Daha Sonra", pendingIntentYes2) 

     .setContentIntent(pIntent).getNotification(); 
     } 
     // .setTicker(" size bir bildirim yolladı.").setSmallIcon(R.drawable.familyiiii) 
     //  .setSubText("Bu mesaj ile artık" + isimler.get(random) + " arayacağın bir görevin oldu.") 
     NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); 
     noti.flags |= Notification.FLAG_AUTO_CANCEL; 
     notificationManager.notify(0, noti); 
     setalarm(context); 

     }while (listDataBoya.get(secilmissayı) == "#1eac02"){////Here , is the problem. 
     String table = "people_table"; 
     ContentValues productDetailsContentValues = new ContentValues(); 
     productDetailsContentValues.put("boya", "#1eac02"); 
     String where = " id = " + listDataId.get(secilmissayı); 
     mDatabaseHelper.update(table, productDetailsContentValues, where , null); 


     Intent yesReceive = new Intent(); 
     yesReceive.setAction(AppConstant.YES_ACTION); 
     PendingIntent pendingIntentYes = PendingIntent.getBroadcast(context, 12345, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT); 


     Intent intenttt = new Intent(context, AnaMenu.class); 
     PendingIntent pIntent = PendingIntent.getActivity(context, 0, intenttt, 0); 


     Intent yesReceive2 = new Intent(); 
     yesReceive2.setAction(AppConstant.STOP_ACTION); 
     PendingIntent pendingIntentYes2 = PendingIntent.getBroadcast(context, 12345, yesReceive2, PendingIntent.FLAG_UPDATE_CURRENT); 
     // Build notification 
     // Actions are just fake 
     Notification noti = null; 
     if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { 
     noti = new Notification.Builder(context) 
     .setTicker("AYNIKİŞİAYNIKİŞİAYNIKİŞİAYNIKİŞİAYNIKİŞİAYNIKİŞİ") 
     .setContentTitle("AYNIKİŞİAYNIKİŞİAYNIKİŞİAYNIKİŞİAYNIKİŞİ") 
     .setContentText(listData.get(secilmissayı)+" i arama zamanı") 
     .setSmallIcon(R.drawable.familyiiii) 

     .addAction(R.drawable.cal, "Ara", pendingIntentYes) 
     .addAction(R.drawable.se, "Daha Sonra", pendingIntentYes2) 

     .setContentIntent(pIntent).getNotification(); 
     } 
     // .setTicker(" size bir bildirim yolladı.").setSmallIcon(R.drawable.familyiiii) 
     //  .setSubText("Bu mesaj ile artık" + isimler.get(random) + " arayacağın bir görevin oldu.") 
     NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); 
     noti.flags |= Notification.FLAG_AUTO_CANCEL; 
     notificationManager.notify(0, noti); 
     setalarm(context); 




     } 

您已經在全局聲明瞭隨機數,所以在整個過程中它不會改變。

+0

對不起,我不明白你的意思是什麼,隨機放置在本地。我把你的代碼複製到我的broadcastreceiver.It –

+0

隨機數字發生器應該在if語句裏 –

+0

我刷新了你的代碼,你是不是這個意思?因爲它沒有工作。如果我做錯了。只要說我是begrenerner在broadcastReceiver。 –