1. 程式人生 > >Android receiver 多次啟動相同Activity

Android receiver 多次啟動相同Activity

在Receiver中多次啟動相同的Activity,如果Intent是相同的是不會重新建立Activity的,只有Intent內容不同時才會啟動多個Activity。

TestActivity.java 程式碼段:

package com.example.gonglt1.ltestapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

/**
 * Created by gonglt1 on 2017/2/6.
 */

public class TestActivity extends Activity {
    private static final String TAG = "glttest";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e(TAG,"TestActivity :: onCreate");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG,"TestActivity :: onResume");
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.e(TAG,"TestActivity :: onNewIntent");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e(TAG,"TestActivity :: onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e(TAG,"TestActivity :: onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e(TAG,"TestActivity :: onDestroy");
    }
}

TestReceiver.java 程式碼段:

package com.example.gonglt1.ltestapp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Handler;
import android.util.Log;

/**
 * Created by gonglt1 on 2017/1/22.
 */

public class TestReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("glttest","TestReceiver :: onReceive");
        Intent intent1 = new Intent(context, TestService.class);
        context.startService(intent1);
    }   
}

TestService.java 程式碼:

package com.example.gonglt1.ltestapp;

import android.app.Service;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

/**
 * Created by gonglt1 on 2017/2/6.
 */

public class TestService extends Service{

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
    }

    private static int i = 0;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
            Log.e("glttest","TestService :: onStartCommmand");
            Intent intent1 = new Intent(this, TestActivity.class);
            intent1.setData(ContentUris.withAppendedId( Uri.parse("content://" + "com" + "/instances"), i++));
            intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
            getApplicationContext().startActivity(intent1);
        return super.onStartCommand(intent, flags, startId);
    }
}

通過cmd命令列傳送廣播:

adb shell am broadcast -a com.example.gonlt1.ltestapp.CMD_SEND_BROADCAST

檢視log日誌輸出如下:

02-06 16:41:05.681 31041-31041/com.example.gonglt1.ltestapp E/glttest: TestReceiver :: onReceive
02-06 16:41:05.687 31041-31041/com.example.gonglt1.ltestapp E/glttest: TestService :: onStartCommmand
02-06 16:41:05.727 31041-31041/com.example.gonglt1.ltestapp E/glttest: TestActivity :: onCreate
02-06 16:41:05.732 31041-31041/com.example.gonglt1.ltestapp E/glttest: TestActivity :: onResume
02-06 16:41:11.148 31041-31041/com.example.gonglt1.ltestapp E/glttest: TestReceiver :: onReceive
02-06 16:41:11.149 31041-31041/com.example.gonglt1.ltestapp E/glttest: TestService :: onStartCommmand
02-06 16:41:11.158 31041-31041/com.example.gonglt1.ltestapp E/glttest: TestActivity :: onPause
02-06 16:41:11.169 31041-31041/com.example.gonglt1.ltestapp E/glttest: TestActivity :: onCreate
02-06 16:41:11.175 31041-31041/com.example.gonglt1.ltestapp E/glttest: TestActivity :: onResume
02-06 16:41:11.240 31041-31041/com.example.gonglt1.ltestapp E/glttest: TestActivity :: onStop

如果註釋掉TestService.java onStartCommand()方法中的這句程式碼:

   Log.e("glttest","TestService :: onStartCommmand");
        Intent intent1 = new Intent(this, TestActivity.class);

//            intent1.setData(ContentUris.withAppendedId( Uri.parse("content://" + "com" + "/instances"), i++));

        intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
        getApplicationContext().startActivity(intent1);

再次在cmd命令列中多次傳送廣播,log輸出如下:

02-06 16:58:45.849 31041-31041/com.example.gonglt1.ltestapp E/glttest: TestReceiver :: onReceive
02-06 16:58:45.851 31041-31041/com.example.gonglt1.ltestapp E/glttest: TestService :: onStartCommmand
02-06 16:58:45.894 31041-31041/com.example.gonglt1.ltestapp E/glttest: TestActivity :: onCreate
02-06 16:58:45.903 31041-31041/com.example.gonglt1.ltestapp E/glttest: TestActivity :: onResume
02-06 16:58:48.840 31041-31041/com.example.gonglt1.ltestapp E/glttest: TestReceiver :: onReceive
02-06 16:58:48.840 31041-31041/com.example.gonglt1.ltestapp E/glttest: TestService :: onStartCommmand
02-06 16:58:54.755 31041-31041/com.example.gonglt1.ltestapp E/glttest: TestReceiver :: onReceive
02-06 16:58:54.756 31041-31041/com.example.gonglt1.ltestapp E/glttest: TestService :: onStartCommmand

從log中看到,只會啟動一次TestActivity。