复选框首选项在broadcastReceiver中使用以进行通知

时间:2017-09-04 17:45:08

标签: android preferenceactivity

我想在我的BroadcastReceiver中使用我的通知复选框。我设置所有复选框,但我无法阻止振动,我无法控制我的通知。我想停止振动。我的复选框首选项是false.İf复选框首选项为true。振动我的手机。我怎样才能做到这一点? (我是Preference活动的初学者)

我的设置xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">


    <CheckBoxPreference
        android:defaultValue="true"
        android:key="vibrate"
        android:summary="Telefona görev geldiğinde titreşim yollanır."
        android:title="Titreşim"
     >
    </CheckBoxPreference>
    <CheckBoxPreference
        android:defaultValue="true"
        android:key="notify"
        android:summary="Telefona görev geldiğinde ses ile uyarılır."
        android:title="Bildirim sesi"
        >
    </CheckBoxPreference>


</PreferenceScreen>

我的设置活动(所以PreferenceActivity):

public class Ayarlar extends PreferenceActivity {


    boolean autovibrate;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);
getListView().setBackgroundColor(getResources().getColor(R.color.yüz));}


    public void onStart(Intent intent, int startId) {
        getPrefs();
    }

    private void getPrefs() {
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(getBaseContext());

        autovibrate = prefs.getBoolean("vibrate", true);
    }


    }

我的广播接收器:

    public class Alarm extends BroadcastReceiver {
      cancelAlarm(context);

            setalarm(context);
    }
    It is in a if loop.
      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.alarm_24dp)

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

                            .setContentIntent(pIntent).getNotification();
                }

                NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
                noti.flags |= Notification.FLAG_AUTO_CANCEL;
                noti.defaults |= Notification.DEFAULT_ALL;

                noti.defaults |= Notification.DEFAULT_VIBRATE;

                notificationManager.notify(0, noti);
I tried achive settings activity                
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);




            }

1 个答案:

答案 0 :(得分:1)

看起来您需要从共享首选项中获取负责振动的属性,具体取决于它的值更改通知设置。将它添加到Alarm类中的代码中:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
Boolean vibrate = prefs.getBoolean("vibrate", true);

noti.defaults |= Notification.DEFAULT_SOUND;
if (vibrate) {
    noti.defaults |= Notification.DEFAULT_VIBRATE;
}