質問
お世話になります。
Androidアプリにおいて、Seriveを、Activityが動作しているプロセスとは異なるプロセスで
動作させたとき、Serviceが起動しているプロセスにもLooperとメインスレッドが存在しているようですが、これは一体どのソースコード(ファイル)の、どの処理によって生成されているのでしょうか?
また、このServiceが起動しているプロセスで動作しているメインスレッドはどのような処理を
行うのでしょうか?
Serviceのライフサイクルメソッドは、ActivityThreadにより呼び出されていることは
確認済みです。
また、ActivityとServiceが同一のプロセス内で動作しているときは、
Looperは一つしかないことを確認しています。
該当のソースコード
■Androidマニフェスト
<service android:name=".TestService" android:process=":testprocess"></service>
■MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ Intent intent = null; Button btn = null; private boolean mode = false; private static final String TAG = "servicedebug"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.i(TAG, "MainActivity#onCreate() getMainLooper().toString() = " + getMainLooper().toString()); Log.i(TAG, "MainActivity#onCreate() getMainLooper().getThread().hashCode() = " + getMainLooper().getThread().hashCode()); btn = (Button)findViewById(R.id.service_btn); btn.setOnClickListener(this); // サービスクラスを指定 intent = new Intent(this, TestService.class); } public void onClick(View v) { // TODO Auto-generated method stub if (v.getId() == R.id.service_btn) { if (!mode) { // サービスの起動 startService(intent); btn.setText("停止"); mode = true; } else { // サービスの停止 stopService(intent); btn.setText("起動"); mode = false; } } } } ```------------------------------ ■TestService.java ```ここに言語を入力 import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; public class TestService extends Service { private static final String TAG = "servicedebug"; int testval = getTrace("TestService#init"); public int getTrace(String s){ // エラー // Log.i(TAG, "TestService#getMainLooper().toString()" + getMainLooper().toString()); // StackTraceElement[] ste = new Throwable().getStackTrace(); StackTraceElement[] ste = Thread.currentThread().getStackTrace(); for (int i = 0; i < ste.length; i++) { Log.i(TAG, s + " ste[" + i + "] = " + ste[i]); } return 1; } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } public TestService(){ Log.i(TAG, "TestService#コンストラクタが呼ばれました"); // エラー // Log.i(TAG, "TestService#TestService() getMainLooper().toString()" + getMainLooper().toString()); } // 開始時にコール @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub super.onStart(intent, startId); Log.i(TAG, "TestService#onStartCommand getMainLooper().toString() = " + getMainLooper().toString()); Log.i(TAG, "TestService#onStartCommand() getMainLooper().getThread().hashCode() = " + getMainLooper().getThread().hashCode()); return super.onStartCommand(intent, flags, startId); } @Override public void onCreate() { super.onCreate(); Log.i(TAG, "TestService#onCreate() getMainLooper().toString() = " + getMainLooper().toString()); Log.i(TAG, "TestService#onCreate() getMainLooper().getThread().hashCode() = " + getMainLooper().getThread().hashCode()); getTrace("TestService#onCreate()"); } // 終了時にコール @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Log.i(TAG, "TestService#onDestroy()"); } }
試したこと
C:\AndroidSDK\sources\android-25\androidの配下を"Looper.prepare"または"Looper.loop"でgrepしたのですが、
Serviceを別のプロセスで起動させたときに、Looper.prepare()やLooper.loop()を行っている処理が見当たりませんでした。
上記テストコードを実行したときのログは下記の通りです。
■異なるプロセスの場合
Looperとスレッドは、Activity側とService側のプロセスで異なっていました。
04-06 01:38:29.312 28385-28385/? I/servicedebug: MainActivity#onCreate() getMainLooper().toString() = Looper (main, tid 1) {d9a2c5a}
04-06 01:38:29.312 28385-28385/? I/servicedebug: MainActivity#onCreate() getMainLooper().getThread().hashCode() = 197426827
・・・
04-06 01:39:23.859 29029-29029/? I/servicedebug: TestService#コンストラクタが呼ばれました
04-06 01:39:23.860 29029-29029/? I/servicedebug: TestService#onCreate() getMainLooper().toString() = Looper (main, tid 1) {7db8c33}
04-06 01:39:23.860 29029-29029/? I/servicedebug: TestService#onCreate() getMainLooper().getThread().hashCode() = 262433776
■同一プロセス(process属性なし : <service android:name=".TestService"></service> )の場合
Looperとスレッドは、Activity側とService側のプロセスで同一でした。
04-06 01:50:33.157 32111-32111/? I/servicedebug: MainActivity#onCreate() getMainLooper().toString() = Looper (main, tid 1) {d9a2c5a}
04-06 01:50:33.157 32111-32111/? I/servicedebug: MainActivity#onCreate() getMainLooper().getThread().hashCode() = 197426827
・・・
04-06 01:50:37.711 32111-32111/ I/servicedebug: TestService#コンストラクタが呼ばれました
04-06 01:50:37.711 32111-32111/ I/servicedebug: TestService#onCreate() getMainLooper().toString() = Looper (main, tid 1) {d9a2c5a}
04-06 01:50:37.711 32111-32111/ I/servicedebug: TestService#onCreate() getMainLooper().getThread().hashCode() = 197426827
補足情報(FW/ツールのバージョンなど)
Android Studio 3.0.1
テスト端末 : AndroidスマホF-02H / Android 7.1.1

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/16 14:49
2018/04/17 05:16
2018/04/19 15:39