۱۳۹۵/۰۶/۲۱, ۰۷:۵۴ ب.ظ
سلام دوست عزیز
من کد تنظیمات که خودم ساختم رو بهتون میدم واس نمونه
فقط من در این تنظیمات به جای ترو و فلس از اعداد استفاده کردم
یه PreferenceActivity بساز برای ذخیره حالت
کد صفحه تنظیمات
کد کلاس صفحه تنظیمات
اجرای کد در اکتیویتی اصلی که چک میکنه اگر عدد ذخیره شده 1 باشه نوتیفکشن فعال میشه اگر 0 باشه غیر فعال میشه
امیدوارم مشکلتون حل بشه
من کد تنظیمات که خودم ساختم رو بهتون میدم واس نمونه
فقط من در این تنظیمات به جای ترو و فلس از اعداد استفاده کردم
یه PreferenceActivity بساز برای ذخیره حالت
کد پیاچپی:
public class SaveSetting extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
SharedPreferences shere = PreferenceManager.getDefaultSharedPreferences(this);
String notifection = shere.getString("not", "");
}
}
کد صفحه تنظیمات
کد پیاچپی:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/back"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="فعال کردن نوتیفکشن"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ذخیره" />
</LinearLayout>
کد کلاس صفحه تنظیمات
کد پیاچپی:
public class SettingApp extends Activity {
SharedPreferences sh;
String chnum;
Button btnsave;
ToggleButton ch;
Editor ed;
Boolean bl;
private String nup;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.setting);
SharedPreferences sh2 = getSharedPreferences("SaveSetting", MODE_PRIVATE);
Editor editorco2 = sh2.edit();
chnum = sh2.getString("not", "");
ch = (ToggleButton)findViewById(R.id.toggleButton1);
btnsave = (Button)findViewById(R.id.button1);
if (chnum.equals("")){
chnum = "0";
}
boolean isChecked = false;
if (chnum.equals("1")) {
ch.setChecked(true);
}
btnsave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SharedPreferences sharedata1 = getSharedPreferences("SaveSetting", MODE_PRIVATE);
Editor ed1 = sharedata1.edit();
if (ch.isChecked()) {
nup="1";
} else {
nup="0";
}
ed1.putString("not", nup);
Toast.makeText(getApplication(), "دادها ذخیره شد",Toast.LENGTH_LONG).show();
ed1.commit();
}
});
}
}
اجرای کد در اکتیویتی اصلی که چک میکنه اگر عدد ذخیره شده 1 باشه نوتیفکشن فعال میشه اگر 0 باشه غیر فعال میشه
کد پیاچپی:
sh = getSharedPreferences("SaveSetting", MODE_PRIVATE);
ed= sh.edit();
String tools = sh.getString("not", "");
if (tools.equals("1")) {
NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher) // notification icon
.setContentTitle("مطلب تصادفی") // title for notification
.setContentText("برای خواندن این مطلب ضربه بزنید") // message for notification
.setAutoCancel(true); // clear notification after click
Intent browserIntent = new Intent(this, Viewnotif.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, browserIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pi);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}
امیدوارم مشکلتون حل بشه
همه آدما برای رسیدن به جایی که هستند از تجربه دیگران استفاده کردند
پس چرا ما از اندک تجربه ای که داریم برای راهنمایی دیگران استفاده نکنیم؟؟
پس چرا ما از اندک تجربه ای که داریم برای راهنمایی دیگران استفاده نکنیم؟؟