<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
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>
public class Utils { public static final String EXTRA_KEY_OUT = "EXTRA_OUT"; public static final String ACTION_MYINTENTSERVICE = "ru.timgor.alerttest.RESPONSE"; public static final String TAG = "AlertTest"; public static final String SUCCESS = "success"; public static final String AUTOMATIC = "chbAutomatic"; }
compile 'com.google.android.gms:play-services-gcm:8.1.0'
private GcmNetworkManager mGcmNetworkManager;
mGcmNetworkManager = GcmNetworkManager.getInstance(this); setAutoStart(true);
public void setAutoStart(boolean isOn){ if(isOn){ Log.d(Utils.TAG, " "); Random myRandom = new Random(); Bundle bundle = new Bundle(); bundle.putInt("randomNum", myRandom.nextInt(10)); PeriodicTask periodicTask = new PeriodicTask.Builder() .setService(AutomaticService.class) .setTag("PeriodicTask") .setPeriod(30) .setPersisted(true) .setExtras(bundle) .setRequiredNetwork(Task.NETWORK_STATE_CONNECTED) .setRequiresCharging(false) .build(); mGcmNetworkManager.schedule(periodicTask); } else { Log.d(Utils.TAG, " "); mGcmNetworkManager.cancelAllTasks(AutomaticService.class); }
import android.content.Intent; import android.content.SharedPreferences; import android.util.Log; import com.google.android.gms.gcm.GcmNetworkManager; import com.google.android.gms.gcm.GcmTaskService; import com.google.android.gms.gcm.TaskParams; public class AutomaticService extends GcmTaskService { @Override public int onRunTask(TaskParams taskParams) { Log.d(Utils.TAG, " . "); Log.d(Utils.TAG, " : "+ taskParams.getExtras().getInt("randomNum")); if (!verify()) { Log.d(Utils.TAG, "AUTO. "); Intent responseIntent = new Intent(); responseIntent.setAction(Utils.ACTION_MYINTENTSERVICE); responseIntent.addCategory(Intent.CATEGORY_DEFAULT); responseIntent.putExtra(Utils.EXTRA_KEY_OUT, false); Log.d(Utils.TAG, " "); sendBroadcast(responseIntent); } else { Log.d(Utils.TAG, "AUTO. "); } return GcmNetworkManager.RESULT_SUCCESS; } public boolean verify(){ SharedPreferences settings = getSharedPreferences(Utils.TAG, MODE_PRIVATE); boolean success = settings.getBoolean("success", false); return success; } }
public class MyBroadRec extends BroadcastReceiver { public int qnt = 0; @Override public void onReceive(Context context, Intent intent) { Boolean result = intent.getBooleanExtra(Utils.EXTRA_KEY_OUT, false); Intent intentRec = new Intent(MainActivity.this, ManualService.class); if(!result && qnt<20){ Log.d(Utils.TAG, " â„– "+qnt); qnt++; try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } startService(intentRec); } else { qnt=0; } } }
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); chbAuto = (CheckBox)findViewById(R.id.chb_Auto); chbVIP = (CheckBox)findViewById(R.id.chb_VIP); btnManual = (Button)findViewById(R.id.btn_Manual); mGcmNetworkManager = GcmNetworkManager.getInstance(this); sPref = getSharedPreferences(Utils.TAG, MODE_PRIVATE); editor = sPref.edit(); editor.putBoolean(Utils.AUTOMATIC, chbAuto.isChecked()); editor.putBoolean(Utils.SUCCESS, chbVIP.isChecked()); editor.commit(); chbAuto.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { editor.putBoolean(Utils.AUTOMATIC, isChecked); editor.commit(); setAutoStart(isChecked); } }); chbVIP.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { editor.putBoolean(Utils.SUCCESS, isChecked); editor.commit(); } }); btnManual.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d(Utils.TAG, " "); Intent intent = new Intent(MainActivity.this, ManualService.class); startService(intent); } }); MyBroadRec myBroadRec = new MyBroadRec(); IntentFilter intentFilter = new IntentFilter(Utils.ACTION_MYINTENTSERVICE); intentFilter.addCategory(Intent.CATEGORY_DEFAULT); registerReceiver(myBroadRec, intentFilter); setAutoStart(true); }
import android.app.IntentService; import android.content.Intent; import android.content.SharedPreferences; import android.util.Log; public class ManualService extends IntentService { public ManualService() { super("ManualService"); } @Override protected void onHandleIntent(Intent intent) { if (!verify()) { Intent responseIntent = new Intent(); responseIntent.setAction(Utils.ACTION_MYINTENTSERVICE); responseIntent.addCategory(Intent.CATEGORY_DEFAULT); responseIntent.putExtra(Utils.EXTRA_KEY_OUT, false); sendBroadcast(responseIntent); Log.d(Utils.TAG, "MANUAL. "); } else { Log.d(Utils.TAG, "MANUAL. "); } } public boolean verify(){ SharedPreferences settings = getSharedPreferences(Utils.TAG, MODE_PRIVATE); boolean success = settings.getBoolean(Utils.SUCCESS, false); return success; } }
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; 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; import com.google.android.gms.gcm.GcmNetworkManager; import com.google.android.gms.gcm.PeriodicTask; import com.google.android.gms.gcm.Task; import java.util.Random; public class MainActivity extends AppCompatActivity { CheckBox chbAuto, chbVIP; Button btnManual; SharedPreferences sPref; SharedPreferences.Editor editor; private GcmNetworkManager mGcmNetworkManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); chbAuto = (CheckBox)findViewById(R.id.chb_Auto); chbVIP = (CheckBox)findViewById(R.id.chb_VIP); btnManual = (Button)findViewById(R.id.btn_Manual); mGcmNetworkManager = GcmNetworkManager.getInstance(this); sPref = getSharedPreferences(Utils.TAG, MODE_PRIVATE); editor = sPref.edit(); editor.putBoolean(Utils.AUTOMATIC, chbAuto.isChecked()); editor.putBoolean(Utils.SUCCESS, chbVIP.isChecked()); editor.commit(); chbAuto.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { editor.putBoolean(Utils.AUTOMATIC, isChecked); editor.commit(); setAutoStart(isChecked); } }); chbVIP.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { editor.putBoolean(Utils.SUCCESS, isChecked); editor.commit(); } }); btnManual.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d(Utils.TAG, " "); Intent intent = new Intent(MainActivity.this, ManualService.class); startService(intent); } }); MyBroadRec myBroadRec = new MyBroadRec(); IntentFilter intentFilter = new IntentFilter(Utils.ACTION_MYINTENTSERVICE); intentFilter.addCategory(Intent.CATEGORY_DEFAULT); registerReceiver(myBroadRec, intentFilter); setAutoStart(true); } public class MyBroadRec extends BroadcastReceiver { public int qnt = 0; @Override public void onReceive(Context context, Intent intent) { Boolean result = intent.getBooleanExtra(Utils.EXTRA_KEY_OUT, false); Intent intentRec = new Intent(MainActivity.this, ManualService.class); if(!result && qnt<20){ Log.d(Utils.TAG, " â„– "+qnt); qnt++; try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } startService(intentRec); } else { qnt=0; } } } public void setAutoStart(boolean isOn){ if(isOn){ Log.d(Utils.TAG, " "); Random myRandom = new Random(); Bundle bundle = new Bundle(); bundle.putInt("randomNum", myRandom.nextInt(10)); PeriodicTask periodicTask = new PeriodicTask.Builder() .setService(AutomaticService.class) .setTag("PeriodicTask") .setPeriod(30) .setPersisted(true) .setExtras(bundle) .setRequiresCharging(false) .setRequiredNetwork(Task.NETWORK_STATE_CONNECTED) .build(); mGcmNetworkManager.schedule(periodicTask); } else { Log.d(Utils.TAG, " "); mGcmNetworkManager.cancelAllTasks(AutomaticService.class); } } }
import android.content.Intent; import android.content.SharedPreferences; import android.util.Log; import com.google.android.gms.gcm.GcmNetworkManager; import com.google.android.gms.gcm.GcmTaskService; import com.google.android.gms.gcm.TaskParams; public class AutomaticService extends GcmTaskService { @Override public int onRunTask(TaskParams taskParams) { Log.d(Utils.TAG, " . "); Log.d(Utils.TAG, " : "+ taskParams.getExtras().getInt("randomNum")); if (!verify()) { Log.d(Utils.TAG, "AUTO. "); Intent responseIntent = new Intent(); responseIntent.setAction(Utils.ACTION_MYINTENTSERVICE); responseIntent.addCategory(Intent.CATEGORY_DEFAULT); responseIntent.putExtra(Utils.EXTRA_KEY_OUT, false); Log.d(Utils.TAG, " "); sendBroadcast(responseIntent); } else { Log.d(Utils.TAG, "AUTO. "); } return GcmNetworkManager.RESULT_SUCCESS; } public boolean verify(){ SharedPreferences settings = getSharedPreferences(Utils.TAG, MODE_PRIVATE); boolean success = settings.getBoolean("success", false); return success; } }
import android.app.IntentService; import android.content.Intent; import android.content.SharedPreferences; import android.util.Log; public class ManualService extends IntentService { public ManualService() { super("ManualService"); } @Override protected void onHandleIntent(Intent intent) { if (!verify()) { Intent responseIntent = new Intent(); responseIntent.setAction(Utils.ACTION_MYINTENTSERVICE); responseIntent.addCategory(Intent.CATEGORY_DEFAULT); responseIntent.putExtra(Utils.EXTRA_KEY_OUT, false); sendBroadcast(responseIntent); Log.d(Utils.TAG, "MANUAL. "); } else { Log.d(Utils.TAG, "MANUAL. "); } } public boolean verify(){ SharedPreferences settings = getSharedPreferences(Utils.TAG, MODE_PRIVATE); boolean success = settings.getBoolean(Utils.SUCCESS, false); return success; } }
public class Utils { public static final String EXTRA_KEY_OUT = "EXTRA_OUT"; public static final String ACTION_MYINTENTSERVICE = "ru.timgor.alerttest.RESPONSE"; public static final String TAG = "AlertTest"; public static final String SUCCESS = "success"; public static final String AUTOMATIC = "chbAutomatic"; }
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.4.0' compile 'com.google.android.gms:play-services-gcm:8.1.0' }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="ru.alerttest"> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".AutomaticService" android:permission="com.google.android.gms.permission.BIND_NETWORK_TASK_SERVICE" android:exported="true"> <intent-filter> <action android:name="com.google.android.gms.gcm.ACTION_TASK_READY"/> </intent-filter> </service> <service android:name=".ManualService"/> </application> </manifest>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" 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"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" " android:id="@+id/btn_Manual" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" " android:id="@+id/chb_Auto" android:checked="true"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" " android:id="@+id/chb_VIP" android:checked="true"/> </LinearLayout>
public class MainActivity extends AppCompatActivity { CheckBox chbAuto, chbVIP; Button btnManual; SharedPreferences sPref; SharedPreferences.Editor editor; private GcmNetworkManager mGcmNetworkManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); chbAuto = (CheckBox)findViewById(R.id.chb_Auto); chbVIP = (CheckBox)findViewById(R.id.chb_VIP); btnManual = (Button)findViewById(R.id.btn_Manual); final MyApplicaion app = (MyApplicaion)getApplicationContext(); mGcmNetworkManager = GcmNetworkManager.getInstance(this); final PrefHelper prefHelper = PrefHelper.getInstance(); chbAuto.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { prefHelper.setAutomatic(isChecked); setAutoStart(isChecked); } }); chbVIP.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { prefHelper.setSuccess(isChecked); } }); prefHelper.setAutomatic(chbAuto.isChecked()); prefHelper.setSuccess(chbVIP.isChecked()); btnManual.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d(Utils.TAG, " "); Intent intent = new Intent(MainActivity.this, ManualService.class); startService(intent); } }); MyBroadRec myBroadRec = new MyBroadRec(); IntentFilter intentFilter = new IntentFilter(Utils.ACTION_MYINTENTSERVICE); intentFilter.addCategory(Intent.CATEGORY_DEFAULT); registerReceiver(myBroadRec, intentFilter); setAutoStart(prefHelper.isAutomatic()); } public class MyBroadRec extends BroadcastReceiver { public int qnt = 0; @Override public void onReceive(Context context, Intent intent) { Boolean result = intent.getBooleanExtra(Utils.EXTRA_KEY_OUT, false); Intent intentRec = new Intent(MainActivity.this, ManualService.class); if(!result && qnt<20){ Log.d(Utils.TAG, " â„– "+qnt); qnt++; try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } startService(intentRec); } else { qnt=0; } } } public void setAutoStart(boolean isOn){ if(isOn){ Log.d(Utils.TAG, " "); Random myRandom = new Random(); Bundle bundle = new Bundle(); bundle.putInt(Utils.RANDOM_NUMS, myRandom.nextInt(10)); PeriodicTask periodicTask = new PeriodicTask.Builder() .setService(AutomaticService.class) .setTag("PeriodicTask") .setPeriod(30) .setPersisted(true) .setExtras(bundle) .setRequiresCharging(false) .setRequiredNetwork(Task.NETWORK_STATE_CONNECTED) .build(); mGcmNetworkManager.schedule(periodicTask); } else { Log.d(Utils.TAG, " "); mGcmNetworkManager.cancelAllTasks(AutomaticService.class); } } }
public class PrefHelper { private static PrefHelper mInstance; private static SharedPreferences sPref; private static SharedPreferences.Editor editor; public static void setInstance(Context context){ mInstance = new PrefHelper(context); } public static PrefHelper getInstance(){ return mInstance; } private PrefHelper(Context context){ this.sPref = PreferenceManager.getDefaultSharedPreferences(context); this.editor = this.sPref.edit(); } public boolean isAutomatic() { return sPref.getBoolean(Utils.AUTOMATIC, false); } public void setAutomatic(boolean automatic) { editor.putBoolean(Utils.AUTOMATIC,automatic); editor.apply(); } public boolean isSuccess() { return sPref.getBoolean(Utils.SUCCESS, false); } public void setSuccess(boolean success) { editor.putBoolean(Utils.SUCCESS, success); editor.apply(); } }
public class MyApplicaion extends Application { @Override public void onCreate() { super.onCreate(); PrefHelper.setInstance(this); } }
public class AutomaticService extends GcmTaskService { @Override public int onRunTask(TaskParams taskParams) { Log.d(Utils.TAG, " . "); Log.d(Utils.TAG, " : "+ taskParams.getExtras().getInt(Utils.RANDOM_NUMS)); if (!PrefHelper.getInstance().isSuccess()) { Log.d(Utils.TAG, "AUTO. "); Intent responseIntent = new Intent(); responseIntent.setAction(Utils.ACTION_MYINTENTSERVICE); responseIntent.addCategory(Intent.CATEGORY_DEFAULT); responseIntent.putExtra(Utils.EXTRA_KEY_OUT, false); Log.d(Utils.TAG, " "); sendBroadcast(responseIntent); } else { Log.d(Utils.TAG, "AUTO. "); } return GcmNetworkManager.RESULT_SUCCESS; } }
public class ManualService extends IntentService { public ManualService() { super("ManualService"); } @Override protected void onHandleIntent(Intent intent) { if (!PrefHelper.getInstance().isSuccess()) { Intent responseIntent = new Intent(); responseIntent.setAction(Utils.ACTION_MYINTENTSERVICE); responseIntent.addCategory(Intent.CATEGORY_DEFAULT); responseIntent.putExtra(Utils.EXTRA_KEY_OUT, false); sendBroadcast(responseIntent); Log.d(Utils.TAG, "MANUAL. "); } else { Log.d(Utils.TAG, "MANUAL. "); } } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="ru.cse.alerttest"> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:name=".MyApplicaion" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".AutomaticService" android:permission="com.google.android.gms.permission.BIND_NETWORK_TASK_SERVICE" android:exported="true"> <intent-filter> <action android:name="com.google.android.gms.gcm.ACTION_TASK_READY"/> </intent-filter> </service> <service android:name=".ManualService"/> </application> </manifest>
public class Utils { public static final String EXTRA_KEY_OUT = "EXTRA_OUT"; public static final String ACTION_MYINTENTSERVICE = "ru.timgor.alerttest.RESPONSE"; public static final String TAG = "AlertTest"; public static final String SUCCESS = "success"; public static final String AUTOMATIC = "chbAutomatic"; public static final String RANDOM_NUMS = "randomNum"; }
Source: https://habr.com/ru/post/309622/