进程化

2019-11-04  本文已影响0人  TomyZhang

一、基础

IPC机制 -- 基础知识 -- 多进程模式

二、使用

//AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tomorrow.testnetworkcache"
    android:sharedUserId="com.tomorrow.uid.test12345">
    <application
        android:name="com.tomorrow.player.MusicApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:usesCleartextTraffic="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>
        <activity android:name="com.tomorrow.player.MusicActivity"
            android:process=":MusicUI"/>
        <service android:name="com.tomorrow.player.MusicService"
            android:process=":MusicService"/>
        <provider
            android:authorities="com.tomorrow.music.provider"
            android:name="com.tomorrow.player.MusicProvider"
            android:process=":MusicProvider"/>
    </application>
</manifest>

//MusicApplication
public class MusicApplication extends Application {
    private static final String TAG = "MusicApplication";

    @Override
    public void onCreate() {
        super.onCreate();
        String processName = getProcessInfo();
        Log.d(TAG, "zwm, onCreate, thread: " + Thread.currentThread().getName() + ", process: " + processName);

        //init
        if(TextUtils.equals(processName, "com.tomorrow.testnetworkcache:MusicUI")) {
            Log.d(TAG, "zwm, MusicUI process init");

        } else if(TextUtils.equals(processName, "com.tomorrow.testnetworkcache:MusicProvider")) {
            Log.d(TAG, "zwm, MusicProvider process init");

        } else if(TextUtils.equals(processName, "com.tomorrow.testnetworkcache:MusicService")) {
            Log.d(TAG, "zwm, MusicService process init");

        }
    }

    public static String getProcessInfo() {
        String processName = null;
        try {
            File file = new File("/proc/" + android.os.Process.myPid() + "/" + "cmdline");
            BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
            processName = bufferedReader.readLine().trim();
            bufferedReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return processName;
    }
}

//MainActivity
public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "zwm, onCreate");
        setContentView(R.layout.activity_main);
        getProcessName();
        getPidAndUid();
        getMemoryInfo();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(MainActivity.this, MusicActivity.class);
                startActivity(intent);
            }
        }, 1000);
    }

    private void getProcessName() {
        String processName = null;
        try {
            File file = new File("/proc/" + android.os.Process.myPid() + "/" + "cmdline");
            BufferedReader mBufferedReader = new BufferedReader(new FileReader(file));
            processName = mBufferedReader.readLine().trim();
            mBufferedReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.d(TAG, "zwm, processName: " + processName);
    }

    private void getPidAndUid() {
        int pid = android.os.Process.myPid();
        int uid = android.os.Process.myUid();
        Log.d(TAG, "zwm, pid: " + pid + ", uid: " + uid);
    }

    private void getMemoryInfo(){
        Runtime rt = Runtime.getRuntime();
        long maxMemory = rt.maxMemory();
        Log.d(TAG, "zwm, maxMemory:" +  Long.toString(maxMemory/(1024*1024)));
        ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        Log.d(TAG, "zwm, getMemoryClass:" + Long.toString(activityManager.getMemoryClass()));
        Log.d(TAG, "zwm, getLargeMemoryClass:" + Long.toString(activityManager.getLargeMemoryClass()));
    }
}

//MusicActivity
public class MusicActivity extends Activity {
    private static final String TAG = "MusicActivity";
    private IMusic mMusic;

    private ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d(TAG, "zwm, onServiceConnected, thread: " + Thread.currentThread().getName() + ", process: " + MusicApplication.getProcessInfo());
            mMusic = IMusic.Stub.asInterface(service);
            Log.d(TAG, "zwm, service: " + service + ", mMusic: " + mMusic);
            playMusic();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d(TAG, "zwm, onServiceDisconnected, thread: " + Thread.currentThread().getName() + ", process: " + MusicApplication.getProcessInfo());
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "zwm, onCreate, thread: " + Thread.currentThread().getName() + ", process: " + MusicApplication.getProcessInfo());
        bindMusicService();
    }

    private void bindMusicService() {
        Log.d(TAG, "zwm, bindMusicService, thread: " + Thread.currentThread().getName() + ", process: " + MusicApplication.getProcessInfo());
        Intent intent = new Intent(this, MusicService.class);
        bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
    }

    private void playMusic() {
        Log.d(TAG, "zwm, playMusic, thread: " + Thread.currentThread().getName() + ", process: " + MusicApplication.getProcessInfo());
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.d(TAG, "zwm, run, thread: " + Thread.currentThread().getName() + ", process: " + MusicApplication.getProcessInfo());
                getContentResolver().query(Uri.parse(MusicProvider.CONTENT_URI), null, null, null, null);
                try {
                    mMusic.play(999);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

//MusicService
public class MusicService extends Service {
    private static final String TAG = "MusicService";

    private final IMusic.Stub mMusic = new IMusic.Stub() {
        @Override
        public void play(int id) throws RemoteException {
            synchronized (this) {
                Log.d(TAG, "zwm, play music, id: " + id + ", thread: " + Thread.currentThread().getName() + ", process: " + MusicApplication.getProcessInfo());
            }
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "zwm, onCreate, thread: " + Thread.currentThread().getName() + ", process: " + MusicApplication.getProcessInfo());
        Log.d(TAG, "zwm, mMusic: " + mMusic);
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "zwm, onBind, thread: " + Thread.currentThread().getName() + ", process: " + MusicApplication.getProcessInfo());
        return mMusic;
    }
}

//IMusic
interface IMusic {
    void play(in int id);
}

//MusicProvider
public class MusicProvider extends ContentProvider {
    private static final String TAG = "MusicProvider";
    public static final String CONTENT_URI = "content://com.tomorrow.music.provider";

    @Override
    public boolean onCreate() {
        Log.d(TAG, "zwm, onCreate, thread: " + Thread.currentThread().getName() + ", process: " + MusicApplication.getProcessInfo());
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        Log.d(TAG, "zwm, query uri: " + uri + ", thread: " + Thread.currentThread().getName() + ", process: " + MusicApplication.getProcessInfo());
        return null;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        return null;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        return 0;
    }
}

//运行输出log
2019-11-04 16:44:56.345 28248-28248/com.tomorrow.testnetworkcache D/MusicApplication: zwm, onCreate, thread: main, process: com.tomorrow.testnetworkcache
2019-11-04 16:44:56.387 28248-28248/com.tomorrow.testnetworkcache D/MainActivity: zwm, onCreate
2019-11-04 16:44:56.493 28248-28248/com.tomorrow.testnetworkcache D/MainActivity: zwm, processName: com.tomorrow.testnetworkcache
2019-11-04 16:44:56.493 28248-28248/com.tomorrow.testnetworkcache D/MainActivity: zwm, pid: 28248, uid: 10399
2019-11-04 16:44:56.493 28248-28248/com.tomorrow.testnetworkcache D/MainActivity: zwm, maxMemory:256
2019-11-04 16:44:56.493 28248-28248/com.tomorrow.testnetworkcache D/MainActivity: zwm, getMemoryClass:256
2019-11-04 16:44:56.493 28248-28248/com.tomorrow.testnetworkcache D/MainActivity: zwm, getLargeMemoryClass:512
2019-11-04 16:44:57.864 28271-28271/com.tomorrow.testnetworkcache:MusicUI D/MusicApplication: zwm, onCreate, thread: main, process: com.tomorrow.testnetworkcache:MusicUI
2019-11-04 16:44:57.864 28271-28271/com.tomorrow.testnetworkcache:MusicUI D/MusicApplication: zwm, MusicUI process init
2019-11-04 16:44:57.910 28271-28271/com.tomorrow.testnetworkcache:MusicUI D/MusicActivity: zwm, onCreate, thread: main, process: com.tomorrow.testnetworkcache:MusicUI
2019-11-04 16:44:57.910 28271-28271/com.tomorrow.testnetworkcache:MusicUI D/MusicActivity: zwm, bindMusicService, thread: main, process: com.tomorrow.testnetworkcache:MusicUI
2019-11-04 16:44:58.213 28288-28288/com.tomorrow.testnetworkcache:MusicService D/MusicApplication: zwm, onCreate, thread: main, process: com.tomorrow.testnetworkcache:MusicService
2019-11-04 16:44:58.213 28288-28288/com.tomorrow.testnetworkcache:MusicService D/MusicApplication: zwm, MusicService process init
2019-11-04 16:44:58.218 28288-28288/com.tomorrow.testnetworkcache:MusicService D/MusicService: zwm, onCreate, thread: main, process: com.tomorrow.testnetworkcache:MusicService
2019-11-04 16:44:58.218 28288-28288/com.tomorrow.testnetworkcache:MusicService D/MusicService: zwm, mMusic: com.tomorrow.player.MusicService$1@2591878
2019-11-04 16:44:58.219 28288-28288/com.tomorrow.testnetworkcache:MusicService D/MusicService: zwm, onBind, thread: main, process: com.tomorrow.testnetworkcache:MusicService
2019-11-04 16:44:58.253 28271-28271/com.tomorrow.testnetworkcache:MusicUI D/MusicActivity: zwm, onServiceConnected, thread: main, process: com.tomorrow.testnetworkcache:MusicUI
2019-11-04 16:44:58.254 28271-28271/com.tomorrow.testnetworkcache:MusicUI D/MusicActivity: zwm, service: android.os.BinderProxy@8c2a84c, mMusic: com.tomorrow.player.IMusic$Stub$Proxy@f10095
2019-11-04 16:44:58.255 28271-28271/com.tomorrow.testnetworkcache:MusicUI D/MusicActivity: zwm, playMusic, thread: main, process: com.tomorrow.testnetworkcache:MusicUI
2019-11-04 16:44:58.256 28271-28305/com.tomorrow.testnetworkcache:MusicUI D/MusicActivity: zwm, run, thread: Thread-6, process: com.tomorrow.testnetworkcache:MusicUI
2019-11-04 16:44:58.568 28307-28307/com.tomorrow.testnetworkcache:MusicProvider D/MusicProvider: zwm, onCreate, thread: main, process: com.tomorrow.testnetworkcache:MusicProvider
2019-11-04 16:44:58.572 28307-28307/com.tomorrow.testnetworkcache:MusicProvider D/MusicApplication: zwm, onCreate, thread: main, process: com.tomorrow.testnetworkcache:MusicProvider
2019-11-04 16:44:58.572 28307-28307/com.tomorrow.testnetworkcache:MusicProvider D/MusicApplication: zwm, MusicProvider process init
2019-11-04 16:44:58.576 28307-28320/com.tomorrow.testnetworkcache:MusicProvider D/MusicProvider: zwm, query uri: content://com.tomorrow.music.provider, thread: Binder:28307_2, process: com.tomorrow.testnetworkcache:MusicProvider
2019-11-04 16:44:58.579 28288-28303/com.tomorrow.testnetworkcache:MusicService D/MusicService: zwm, play music, id: 999, thread: Binder:28288_3, process: com.tomorrow.testnetworkcache:MusicService
上一篇下一篇

猜你喜欢

热点阅读