以下のページでAndroidManifest.xmlのreceiverタグに「android:process=":remote"」を追加すると
BroadcastReceiverは別プロセスで動作すると記載してありました。
http://rtaki.blogspot.jp/2010/11/broadcastreceiver.html
これを参考に以下のコードを書いて、AlarmManagerを用いて実際に別プロセスで動くか確認したいのですが、
Activityと同一プロセス(pid=8791)で動いているようです。
AlarmManagerのonReceive()を別プロセスで受け取るにはまだ何か不足しているのでしょうか?
どうかよろしくお願い致します。
【実行結果】 08-14 15:37:09.044 8791-8791/? V/testtest﹕ onCreate() pid=8791 08-14 15:37:11.256 8791-8791/? V/testtest﹕ onReceive() pid=8791 08-14 15:37:14.189 8791-8791/? V/testtest﹕ onReceive() pid=8791 08-14 15:37:19.194 8791-8791/? V/testtest﹕ onReceive() pid=8791 ...省略...
java
1public class MainActivity extends AppCompatActivity { 2 @Override 3 protected void onCreate(Bundle savedInstanceState) { 4 super.onCreate(savedInstanceState); 5 Log.v("testtest", "onCreate() pid=" + android.os.Process.myPid()); 6 setContentView(R.layout.activity_main); 7 8 Hoge hoge = new Hoge(); 9 hoge.startAlarm(getApplicationContext()); 10 } 11}
java
1public class Hoge extends BroadcastReceiver { 2 void startAlarm(Context context) { 3 context.registerReceiver(this, new IntentFilter("fugafuga")); 4 PendingIntent pendingIntent = PendingIntent.getBroadcast( context, 0, new Intent("fugafuga"), 0 ); 5 AlarmManager manager = (AlarmManager)(context.getSystemService(Context.ALARM_SERVICE)); 6 manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 5 * 1000, pendingIntent); 7 } 8 @Override 9 public void onReceive(Context context, Intent intent) { 10 Log.v("testtest", "onReceive() pid=" + android.os.Process.myPid()); 11 } 12} 13
xml
1<manifest xmlns:android="http://schemas.android.com/apk/res/android" 2 package="com.test.alarmmanagertest" > 3 4 <application 5 android:allowBackup="true" 6 android:icon="@mipmap/ic_launcher" 7 android:label="@string/app_name" 8 android:theme="@style/AppTheme" > 9 <activity 10 android:name=".MainActivity" 11 android:label="@string/app_name" > 12 <intent-filter> 13 <action android:name="android.intent.action.MAIN" /> 14 15 <category android:name="android.intent.category.LAUNCHER" /> 16 </intent-filter> 17 </activity> 18 <receiver 19 android:name=".Hoge" 20 android:process=":remote" /> 21 </application> 22</manifest>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/08/18 03:25