Код: Выделить всё
sound = (Switch)findViewById(R.id.sw_sound);
sound.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton cB, boolean state) {
if(state){
ShPrefs.SaveStringPrefs("msound", "yes", context);
}else{
ShPrefs.SaveStringPrefs("msound", "no", context);
}
}
});
Код: Выделить всё
public static void get_last_news(final Context context){
String Url = "url";
SyncHttpClient client = new SyncHttpClient(true, 80, 443);
client.get(Url, new JsonHttpResponseHandler(){
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
super.onSuccess(statusCode, headers, response);
Intent notificationIntent = new Intent(context, NewsActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context,
0, notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
Resources res = context.getResources();
Notification.Builder builder = new Notification.Builder(context);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_launcher)
// большая картинка
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
//.setTicker(res.getString(R.string.warning)) // текст в строке состояния
.setTicker("Последнее китайское предупреждение!")
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
//.setContentTitle(res.getString(R.string.notifytitle)) // Заголовок уведомления
.setContentTitle("Напоминание")
//.setContentText(res.getString(R.string.notifytext))
.setContentText("Пора покормить кота"); // Текст уведомленимя
if(ShPrefs.LoadStringPrefs("msound", "no", context).equals("yes")){
Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(uri);
}
Notification notification = builder.getNotification(); // до API 16
//Notification notification = builder.build();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
super.onFailure(statusCode, headers, responseString, throwable);
Log.e("fail", "--------");
}
});
}
Код: Выделить всё
public static void SaveStringPrefs(String key, String value, Context context) {
SharedPreferences preferences = context.getSharedPreferences("MY_PREF", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, value);
Log.e("SAVE " + key, value);
editor.commit();
}
public static String LoadStringPrefs(String key, String defvalue, Context context) {
SharedPreferences preferences = context.getSharedPreferences("MY_PREF", Activity.MODE_PRIVATE);
preferences.getString(key, defvalue);
Log.e("LoadPreference "+ key, preferences.getString(key, defvalue));
return preferences.getString(key, defvalue);
}
проблема в том, что независимо от настроек звук или есть для всех уведомлени или нет вообще. как это исправить?