Skip to content

Instantly share code, notes, and snippets.

@perqin
Created September 12, 2017 01:33
Show Gist options
  • Save perqin/9c1cd9337fa0f3a9d96cf9f313f90075 to your computer and use it in GitHub Desktop.
Save perqin/9c1cd9337fa0f3a9d96cf9f313f90075 to your computer and use it in GitHub Desktop.
Android components callbacks' running thread
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.perqin.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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=".MainIntentService"
android:exported="false" />
<receiver
android:name=".MainReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="com.perqin.myapplication.MAIN" />
</intent-filter>
</receiver>
<provider
android:name=".MainContentProvider"
android:authorities="com.perqin.myapplication.provider"
android:enabled="true"
android:exported="false"></provider>
</application>
</manifest>
09-12 09:29:08.342 30158-30158/com.perqin.myapplication I/MainActivity: onCreate: Thread ID = 1
09-12 09:29:08.354 30158-30176/com.perqin.myapplication I/MainContentProvider: query: Thread ID = 2374
09-12 09:29:08.389 30158-30193/com.perqin.myapplication I/MainIntentService: onHandleIntent: Thread ID = 2378
09-12 09:29:09.632 30158-30158/com.perqin.myapplication I/MainReceiver: onReceive: Thread ID = 1
package com.perqin.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "onCreate: Thread ID = " + Thread.currentThread().getId());
new Thread(new Runnable() {
@Override
public void run() {
MainIntentService.startAction(MainActivity.this);
sendBroadcast(new Intent("com.perqin.myapplication.MAIN"));
getContentResolver().query(
Uri.parse("content://com.perqin.myapplication.provider/main"),
null, null, null, null
);
}
}).start();
}
}
package com.perqin.myapplication;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
public class MainContentProvider extends ContentProvider {
private static final String TAG = "MainContentProvider";
public MainContentProvider() {
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return -1;
}
@Override
public String getType(Uri uri) {
return "plain/text";
}
@Override
public Uri insert(Uri uri, ContentValues values) {
return null;
}
@Override
public boolean onCreate() {
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
Log.i(TAG, "query: Thread ID = " + Thread.currentThread().getId());
return null;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
return -1;
}
}
package com.perqin.myapplication;
import android.app.IntentService;
import android.content.Intent;
import android.content.Context;
import android.util.Log;
public class MainIntentService extends IntentService {
private static final String TAG = "MainIntentService";
private static final String ACTION_MAIN = "com.perqin.myapplication.action.MAIN";
public MainIntentService() {
super("MainIntentService");
}
public static void startAction(Context context) {
Intent intent = new Intent(context, MainIntentService.class);
intent.setAction(ACTION_MAIN);
context.startService(intent);
}
@Override
protected void onHandleIntent(Intent intent) {
Log.i(TAG, "onHandleIntent: Thread ID = " + Thread.currentThread().getId());
}
}
package com.perqin.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MainReceiver extends BroadcastReceiver {
private static final String TAG = "MainReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "onReceive: Thread ID = " + Thread.currentThread().getId());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment