2016-05-20 54 views
-1

您好我有一個設置活動,其中有一個名爲「顯示預覽」的複選框。我想做的事情是,如果這個複選框被選中,那麼它會發送一些東西給廣播接收器。在廣播接收機中,我想獲得複選框的值,如果它是真的,即複選框被選中,那麼它會在收到新短信時在通知欄中顯示通知。如果複選框的值未選中,則不顯示通知。現在我可以通過共享pref存儲複選框的值,但我沒有在接收器類中獲得該值。如果複選框被選中,然後發送值給廣播接收器

這是我在設置活動代碼:`

CheckBox ch1; 
private SharedPreferences loginPreferences; 
private SharedPreferences.Editor loginPrefsEditor; 
private Boolean saveLogin; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.settings); 
    ch1 = (CheckBox) findViewById(R.id.checkBox1); 
    loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE); 
    loginPrefsEditor = loginPreferences.edit(); 

    saveLogin = loginPreferences.getBoolean("saveLogin", false); 
    if (saveLogin == true) { 
     ch1.setChecked(true); 
    } 
} 
@Override 
protected void onStop() { 
    // TODO Auto-generated method stub 
    super.onStop(); 
    if (ch1.isChecked()) 
    { 
     loginPrefsEditor.putBoolean("saveLogin", true); 
     loginPrefsEditor.commit(); 
     Intent intent = new Intent("my.action.string"); 
     intent.putExtra("checked", "1"); 
     sendBroadcast(intent); 

    } 
else { 
     loginPrefsEditor.clear(); 
     loginPrefsEditor.commit(); 

    } 

正如你看到我寫的代碼,當過使用停止活動,如果這個值被選中則該值應該去接收機類。 現在接收機類這裏是我的代碼:

String action = intent.getAction(); 
    String state = ""; 
    if(action.equals("my.action.string")){ 
     state = intent.getExtras().getString("checked"); 

     } 

最後我檢查,如果狀態的值是1,則通知應該顯示。 這是我在清單文件中的接收器也寫了這對通知

if (state == "1") 
     { 

     NotificationCompat.Builder notify = new NotificationCompat.Builder(context); 
      notify.setSmallIcon(R.drawable.newnotifysmall);    
      notify.setContentTitle(title); 
      notify.setContentText(msgBody); 
      notify.setAutoCancel(true); 
      Notification notification = new Notification(); 
      notification.defaults |= Notification.DEFAULT_VIBRATE; 
      //notify.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000}); 
      notify.setLights(Color.GREEN, 2000, 2000); 
      notify.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); 
      Intent notificationIntent = new Intent(Intent.ACTION_MAIN); 
      notificationIntent.addCategory(Intent.CATEGORY_DEFAULT); 
      notificationIntent.setType("vnd.android-dir/mms-sms"); 
      notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
      PendingIntent intentt = PendingIntent.getActivity(context, 0,notificationIntent, 0); 
      notify.setContentIntent(intentt); 
      NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); 
      notificationManager.notify(0, notify.build()); 

     } 

代碼:

<action android:name="my.action.string"></action> 

但我覺得值不流通TOR接收器類。請幫助

+0

的可能的複製[我如何在Java中比較字符串?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) –

+0

比較???我不是在比較,我只是沒有從活動中獲得價值。我是否需要在接收器中獲得偏好值? –

+0

'state ==「1」'不是你比較'字符串'的方式。 –

回答

1

那麼這裏是你可以做

在設定的活動,什麼的onStop方法intent.putExtra("checked", "1");

將其更改爲intent.putExtra("checked", 1) 取出的「1」的報價爲你只是發送了一些不是一個字符串。

然後在receiver類中,確保你已經聲明並初始化了intent。

String action = intent.getAction(); 
    int state; 
    if(action.equals("my.action.string")){ 
     state = intent.getIntExtra("checked", 1); 
    } 

我所做的是因爲你只需要檢查數字不是字符串,我已經將狀態更改爲整數。當接收到的值1將被存儲在狀態中。

你可能知道這是什麼意思intent.getIntExtra("checked", 0); 這裏0是我設置的默認值。

接下來在通知代碼

if (state == 1){ 
    //all the notification code 
} 

這一切, 希望工程

+0

好吧,讓我檢查這位先生 –

+0

工作最後:) –

+0

很高興它的作品。快樂編碼:) –