手動開始タイマー(エラー処理)

初心者のAndroidプログラマーであり、n歳から500歳のVanka Zhukovは、誰も知らないときに学校に送られ、就寝しませんでした。 彼の同僚と上司が大衆のために去るのを待った後、彼は暗い...茶、さびた出力を持つキーボードからボトルを取り出し、Android Studioを立ち上げて書き始めました。 最初の手紙を表示する前に、彼はSkypeの窓を何回もti病に見つめて断続的にため息をつきました。







こんにちは、親愛なる祖父のハブル・ハブロビッチ! 彼は書いた。 -私はあなたに手紙を書いています。 聖金曜日におめでとう、そして週末に最高の日をお祈りします。」



ヴァンカはスカイプで目を細めて、ハブル・カブロヴィッチを鮮やかに想像しました。 画像は印象的でしたが、ボリュームが大きすぎました。 ヴァンカはため息をつき、書き続けた。

「そして昨日、私は引きずられました。 私は、自動開始と手動制御でタイマーを作成することにしました。 彼は書いて、長い間彼を賞賛しました。 しかし、叔父は、私をひどくscった。 Scられますが、前髪に引きずられません。 彼らはスマートな本と役に立つヒントを与えました。

Uncle Dimezis 、不正確な変数名を強く呪い、変数キーのハードキーキー。 Kodstaylはアイデンティティをscりました。 彼は、恥ずかしくないように書き直したと言った。

叔父のivazhnovAlex837は、バッテリーの不正確な使用のためにscられました。 銃口では、彼女の銃口は滑りませんでしたが、強く眉をひそめました。 彼らは書き直し、恥ずかしくないと言った。

おじさんMetAmfetamin 、慰めたが、他を支えた。 彼は、恥ずかしくないように書き直したと言った。」

ヴァンカは耳の後ろを掻き、鍵をたたき続けました。

「彼らは、マニフェストに記載されているネットワークステータスの変化をキャッチするためにBroadcastReceiverを使用することは不可能だと言いました。

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







彼らは、JobScheduler、GcmNetworkManager、またはSyncAdapterに目を向けると言いました。

私は長い間考えて、 GcmNetworkManagerで停止することにしました。これは、Androidの古いバージョンに適しており、より普遍的であるようだからです。

ヴァンカは昨日読んだ文書の山に目を細めて、あくびをしました。

「まず、UniversalReceiverへのすべての参照を削除しました。 いわば、クラスとして排除されました。 そして、マニフェストからこすりました。 次に、すべてのタグを取り出すサービスクラスを作成しました。



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







次に、gradleに応じて、次の行を追加しました。

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





MainActivityに変更が追加されました。 発表:

 private GcmNetworkManager mGcmNetworkManager;
      
      





onCreateで:

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





そして追加されたメソッド:

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







Habr Khabrovich様再起動(setPersisted)、乱数の送信、ネットワークが接続するまで動作せず(setRequiredNetwork)、充電への接続を必要としません(setRequiresCharging)。 そして、誰かがメソッドに嘘を渡すと、自動操作は停止します。

次に、AutomaticServiceサービスを作成しました。単純ではありませんが、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; } }
      
      







サービスが正常に機能していれば喜ばれ、それを実現する運命でなければ、MainActivityに登録されているBroadcastReceiverを起動します。 彼は10秒に2回、0.1秒の休憩で接続しようとしますが、心に吐き出すと、次のティックまでこのことをスローします。

テストアプリケーションでは、「非常に重要なオプション」のチェックマークを追加しました。 押された場合、タスクは安全に実行されます。

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







そしてonCreateで:



onCreate完全に
  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); }
      
      







この受信機を登録します。」

ヴァンカは汗を拭き、魅力的な霧の小瓶を濃いお茶で目を細め、明らかに首を振って続けました。 「また、タイマーティックを待たずに、手動でタスクを開始したい。 このために、IntentServiceを継承するManualServiceサービスを作成しました。

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







私はアプリケーションを立ち上げ、とても幸せでした。 フライトモードに切り替えてインターネットをオフにすると、自動起動は開始されないと見なされます。 しかし、親愛なる祖父とすべてのクラス:

主な活動
 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"; }
      
      







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







マニフェスト
 <?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>
      
      







概して、親愛なる祖父にとって、タイマーは目が痛いだけの光景でした。 みんなが言うのを待ちます。 心配しないで 私は賢い開発者になりたいので、これからも挑戦していきます。 それでは、健康で楽しい週末をお過ごしください。」

Rolyはキーボードを動かし、曇ったお茶の泡から泡立ち、「公開」ボタンに手を伸ばしました。 少し考えて、「祖父の村へ」というタグを入力しました。 彼は、マウスを耳の後ろに引っ掻き、「Habr to Khabrovich」を追加しました。

研究所の教授は、出版物は楽しい管理者によって管理され、インターネット全体に光ファイバーと銅線に広がっていると述べた。 ヴァンカは勇気を出し、大きな緑色のボタンを押しました。

甘い希望にだまされて、彼は1時間後にぐっすり眠りました...彼はHalf-Life 3を夢見ました。



PS:更新されたコード、尊敬されているコメンテーターshakagamiiParntのコメントによって修正

主な活動
 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); } }
      
      









AutomaticService-自動開始
 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-手動タスク起動
 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>
      
      









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








All Articles