<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="ru.alerttest.MainActivity"> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" " android:id="@+id/checkBox" android:checked="true" android:layout_below="@+id/button" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" " android:id="@+id/button" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" " android:id="@+id/checkBox2" android:checked="true" android:layout_below="@+id/checkBox" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> </RelativeLayout>
CheckBox checkBox, checkBox1; SharedPreferences sPref; SharedPreferences.Editor editor; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); checkBox = (CheckBox)findViewById(R.id.checkBox); sPref = getSharedPreferences("AlertTest",MODE_PRIVATE); editor = sPref.edit(); editor.putBoolean("chbAutomatic", checkBox.isChecked()); editor.putBoolean("success", checkBox.isChecked()); editor.commit(); checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { editor.putBoolean("chbAutomatic", checkBox.isChecked()); editor.commit(); setServiceAlarm(MainActivity.this); } }); checkBox1 = (CheckBox)findViewById(R.id.checkBox2); checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { editor.putBoolean("success", checkBox1.isChecked()); editor.commit(); setServiceAlarm(MainActivity.this); } }); Button button = (Button)findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d("AlertTest", " "); Intent intent = new Intent(MainActivity.this, UniversalService.class); startService(intent); } }); }
public void setServiceAlarm(Context context){ SharedPreferences settings = getSharedPreferences("AlertTest", MODE_PRIVATE); Boolean automaticSynchronize = settings.getBoolean("chbAutomatic", false); Intent intent = new Intent(context, UniversalService.class); PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0); AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); Integer period = 5; if(automaticSynchronize){ alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), period*1000, pendingIntent); Log.d("AlertTest", " , : "+period*1000); } else { Log.d("AlertTest", " "); alarmManager.cancel(pendingIntent); pendingIntent.cancel(); } }
public class MyBroadRec extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Boolean result = intent.getBooleanExtra(UniversalService.EXTRA_KEY_OUT, false); Intent intentRec = new Intent(MainActivity.this, UniversalService.class); if(!result){ Log.d("AlertTest", " "); startService(intentRec); } } }
MyBroadRec myBroadRec = new MyBroadRec(); IntentFilter intentFilter = new IntentFilter(UniversalService.ACTION_MYINTENTSERVICE); intentFilter.addCategory(Intent.CATEGORY_DEFAULT); registerReceiver(myBroadRec, intentFilter); setServiceAlarm(MainActivity.this);
package ru.alerttest; import android.app.IntentService; import android.content.Intent; import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.util.Log; public class UniversalService extends IntentService { public static final String EXTRA_KEY_OUT = "EXTRA_OUT"; public static final String ACTION_MYINTENTSERVICE = "ru.timgor.alerttest.RESPONSE"; public UniversalService() { super("UniversalService"); } @Override protected void onHandleIntent(Intent intent) { Log.d("AlertTest", " "); if(!verify()){ Intent responseIntent = new Intent(); responseIntent.setAction(ACTION_MYINTENTSERVICE); responseIntent.addCategory(Intent.CATEGORY_DEFAULT); responseIntent.putExtra(EXTRA_KEY_OUT, false); Log.d("AlertTest", " "); sendBroadcast(responseIntent); } else{ Log.d("AlertTest", " "); } } public boolean verify(){ SharedPreferences settings = getSharedPreferences("AlertTest", MODE_PRIVATE); Boolean success = settings.getBoolean("success", false); return success; } }
package ru.alerttest; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; import android.widget.Toast; public class UniversalReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.d("AlertTest", " "); Intent intentNew = new Intent(context, MainActivity.class); intentNew.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intentNew); } }
<receiver android:name=".UniversalReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiver>
package ru.alerttest; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; public class MainActivity extends AppCompatActivity { CheckBox checkBox, checkBox1; SharedPreferences sPref; SharedPreferences.Editor editor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); checkBox = (CheckBox)findViewById(R.id.checkBox); sPref = getSharedPreferences("AlertTest",MODE_PRIVATE); editor = sPref.edit(); editor.putBoolean("chbAutomatic", checkBox.isChecked()); editor.putBoolean("success", checkBox.isChecked()); editor.commit(); checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { editor.putBoolean("chbAutomatic", checkBox.isChecked()); editor.commit(); setServiceAlarm(MainActivity.this); } }); checkBox1 = (CheckBox)findViewById(R.id.checkBox2); checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { editor.putBoolean("success", checkBox1.isChecked()); editor.commit(); setServiceAlarm(MainActivity.this); } }); Button button = (Button)findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d("AlertTest", " "); Intent intent = new Intent(MainActivity.this, UniversalService.class); startService(intent); } }); MyBroadRec myBroadRec = new MyBroadRec(); IntentFilter intentFilter = new IntentFilter(UniversalService.ACTION_MYINTENTSERVICE); intentFilter.addCategory(Intent.CATEGORY_DEFAULT); registerReceiver(myBroadRec, intentFilter); setServiceAlarm(MainActivity.this); } public class MyBroadRec extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Boolean result = intent.getBooleanExtra(UniversalService.EXTRA_KEY_OUT, false); Intent intentRec = new Intent(MainActivity.this, UniversalService.class); if(!result){ Log.d("AlertTest", " "); startService(intentRec); } } } public void setServiceAlarm(Context context){ SharedPreferences settings = getSharedPreferences("AlertTest", MODE_PRIVATE); Boolean automaticSynchronize = settings.getBoolean("chbAutomatic", false); Intent intent = new Intent(context, UniversalService.class); PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0); AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); Integer period = 5; if(automaticSynchronize){ alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), period*1000, pendingIntent); Log.d("AlertTest", " , : "+period*1000); } else { Log.d("AlertTest", " "); alarmManager.cancel(pendingIntent); pendingIntent.cancel(); } } }
package ru.alerttest; import android.app.IntentService; import android.content.Intent; import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.util.Log; public class UniversalService extends IntentService { public static final String EXTRA_KEY_OUT = "EXTRA_OUT"; public static final String ACTION_MYINTENTSERVICE = "ru.timgor.alerttest.RESPONSE"; public UniversalService() { super("UniversalService"); } @Override protected void onHandleIntent(Intent intent) { Log.d("AlertTest", " "); if(!verify()){ Intent responseIntent = new Intent(); responseIntent.setAction(ACTION_MYINTENTSERVICE); responseIntent.addCategory(Intent.CATEGORY_DEFAULT); responseIntent.putExtra(EXTRA_KEY_OUT, false); Log.d("AlertTest", " "); sendBroadcast(responseIntent); } else{ Log.d("AlertTest", " "); } } public boolean verify(){ SharedPreferences settings = getSharedPreferences("AlertTest", MODE_PRIVATE); Boolean success = settings.getBoolean("success", false); return success; } }
package ru.alerttest; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; import android.widget.Toast; public class UniversalReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.d("AlertTest", " "); Intent intentNew = new Intent(context, MainActivity.class); intentNew.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intentNew); } }
Source: https://habr.com/ru/post/309408/
All Articles