⬆️ ⬇️

Manual start timer (work on bugs)

Roly Zhukov, a beginner Android programmer, n-tsyati years of age, put into training is not known when, did not go to bed. After waiting for the colleagues and bosses to leave for the mass, he took out a bottle of dark ... tea, a keyboard with a rusty output, launched Android Studio and began to write. Before deducing the first letter, he looked fearfully at the windows of Skype several times and sighed intermittently.







“Hello, dear grandfather Habr Habrovich! - he wrote. - I am writing you a letter. Congratulations on bright Friday, and I wish you all the weekend. "



Roly glanced at Skype and vividly imagined Habr Habrovich. The image was impressive, but too voluminous. Roly sighed and continued to write.

“Yesterday I had a beating. I decided to write my own timer, with automatic start and manual control . I wrote and admired them for a long time. But guys, sternly reprimanded me. Scolded, but for the forelock not dragged. They gave clever books and useful tips.

Uncle Dimezis , swore strongly for non-muted variable names, and for the keys of variables are hard-coded. CodeStyle scolded identity. He said rewrite and do not disgrace.

Uncles ivazhnov and Alex837 blamed for careless use of the battery. In the face, the muzzle of hers did not strike, but they frowned heavily. They said rewrite and do not disgrace.

Uncle MetAmfetamin , comforted, but supported others. He said rewrite and not shame. "

Roly scratched his ear, and continued to knock on the keys.

“They said that it is impossible to use the BroadcastReceiver for catching network state changes, for which it is written in the manifest:

<action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> 


It is impossible to write like that, because the battery is not childish:

Bad code sample
 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> 




They said to look in the direction of JobScheduler or GcmNetworkManager or SyncAdapter.

I thought for a long time and decided to stop at GcmNetworkManager , because it is suitable for older versions of Android and seems more universal to me. ”

Roly glanced at the mountain of documentation read yesterday and yawned.

“I deleted all references to the UniversalReceiver to begin with. Eliminated, so to speak, as a class. And from the manifesto rubbed. Then I created a service class, in which I put all the tags.

')

Utils Service Class
 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"; } 




Further, gradle added a line according to:

 compile 'com.google.android.gms:play-services-gcm:8.1.0' 


Added changes to MainActivity. Announced:

 private GcmNetworkManager mGcmNetworkManager; 


OnCreate:

 mGcmNetworkManager = GcmNetworkManager.getInstance(this); setAutoStart(true); 


And the method added:

setAutoStart
  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); } 




Here, dear Habr Habrovich, since the truth comes true, I declare my intention to create a task that will periodically launch the AutomaticService (setService) service, wear the PeriodicTask (setTag) tag, called every 30 seconds (setPeriod), work after restart (setPersisted), transmit a random number, do not work until the network connects (setRequiredNetwork) and does not require a connection to charge (setRequiresCharging). And if someone sovramshi transmits to the method, the automatic operation stops.

Next, I created the AutomaticService service, and not simple, but inherited from GcmTaskService:

Automaticservice
 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; } } 




If the service has worked well, then we rejoice, and if it’s not destined to be fulfilled, then it launches BroadcastReceiver, which is registered in the MainActivity. Twice as many as ten tries to connect, with a break of 0.1 seconds, and spitting in the hearts throws this thing, until the next tick.

In the test application, I added the checkbox "Very important option". If it is pressed, then the task successfully fulfills.

BroadcastReceiver
  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; } } } 




And in onCreate:



onCreate completely
  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); } 




Register this receiver.

Rolyka wiped off the sweat, glanced at the temptingly sweaty vial of dark tea, shook his head decisively, and continued. “Also, I want to start the task manually, without waiting for the timer tick. For this, I wrote the service ManualService inherited from the IntentService.

ManualService
 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; } } 




I started the application and was very happy. If you go into flight mode and turn off the Internet, then automatic launch does not even think of starting. But dear grandfather and all classes:

MainActivity
 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); } } } 


Automaticservice
 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; } } 




ManualService
 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; } } 




Utils
 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 gradle
 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' } 




Manifest
 <?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> 




activity_main
 <?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> 




In general, dear grandfather, it turned out the timer is just a lovely sight. I'll wait for the guys to say. And don't worry about me. I want to become a smart developer and I will try to continue. I wish you good health and exciting weekend. ”

Roly pushed the keyboard, squirmed out of the misted tea bubble, and reached for the "Publish" button. Thinking a little, I typed in the tag "On the village grandfather." I scratched the mouse behind the ear, and added “Habrich Habrovich.”

Professors from the institute said that the publications are distributed by wire fiber optic and copper, throughout the Internet, controlled by cheerful admins. Roly gathered his courage and pressed a large green button.

Lulled by sweet hopes, he slept soundly an hour later ... He dreamed of Half-Life 3.



PS: Updated code corrected by comments by respected commentators shakagamii and Parnt

MainActivity
 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); } } } 






New class to work with settings
 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(); } } 






Application heir
 public class MyApplicaion extends Application { @Override public void onCreate() { super.onCreate(); PrefHelper.setInstance(this); } } 






AutomaticService - automatic start
 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; } } 






ManualService - manual task launch
 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.   "); } } } 






Manifest
 <?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> 






Utils
 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/



All Articles