如何检查已启动的Intent?

时间:2014-03-31 10:06:16

标签: android android-intent android-activity singleton android-lifecycle

我正在启动一个Activity并从后台恢复该活动,因此我怎么知道该活动已经启动并仍然有效。将应用程序放在后台时,Activity会停止,但是当从后台到前台恢复应用程序时,我怎么知道它已经存在并且不需要重新创建相同Intent的实例。

public static void launchMyActivty() {

Log.d("isActivity Available", ""+MySingleton.isActivityAvailable(masterAct, "com.mypackage", "com.mypackage.ui.Activity"));     // Always returns true becuase activity is defined in Manifest
Log.d("isIntent Available", ""+MySingleton.isIntentAvailable(masterAct, "com.mypackage.android.ENCODE"));         //  Always returns false, I dont know why

            Intent myintent = masterAct.getIntent();
            String encodeCheck = myintent.getStringExtra("ENCODE_DATA");     //// returning null everytime

            ///  com.mypackage.ui.Activity            ///   This is not Default Activity


            Intent intent = new Intent();
            intent.setClassName("com.mypackage","com.mypackage.ui.Activity");

            if (masterAct.getPackageManager().resolveActivity(intent,
                       PackageManager.MATCH_DEFAULT_ONLY) != null) {

              Log.d("Already launched", "Already launched");     // This is always called even for the first time.

        } else {

                         Log.d("Launching for first time", "Launching for first time");  
                      ///This is never called

                        if (encodeCheck == null) {


                          intent.setAction("com.mypackage.android.ENCODE");
                          intent.putExtra("ENCODE_TYPE", "TEXT_TYPE");
                          intent.putExtra ("ENCODE_DATA", "123456789");
                          //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                           masterAct.startActivity(intent);

                        }
        }


 }

请确保我的活动不是默认活动或主活动,它是子活动。

也是从Singleton检查的方法

public static boolean isIntentAvailable(Context context, String action) {
    final PackageManager packageManager = context.getPackageManager();
    final Intent intent = new Intent(action);
    List resolveInfo = packageManager.queryIntentActivities(intent,
            PackageManager.MATCH_DEFAULT_ONLY);
    Log.d("List Actions", ""+resolveInfo);

    if (resolveInfo.size() > 0) {
        return true;
    }
    return false;
}


public static boolean isActivityAvailable(Context context, String packageName,
        String className) {
    final PackageManager packageManager = context.getPackageManager();
    final Intent intent = new Intent();
    intent.setClassName(packageName, className);

    List list = packageManager.queryIntentActivities(intent,
            PackageManager.MATCH_DEFAULT_ONLY);

    return list.size() > 0;
}


public boolean isCallable() {
    Intent intent = new Intent();
    intent.setClassName("com.android.settings.ethernet",
            "com.android.settings.ethernet.EthernetSettingsAML");

    if (getActivity().getPackageManager().resolveActivity(intent,
            PackageManager.MATCH_DEFAULT_ONLY) != null) {
        return true;
    }
    return false;
}

2 个答案:

答案 0 :(得分:0)

似乎您需要的是只运行一个活动实例,您可以做的是添加到清单中的活动声明

android:launchMode="singleInstance"

...其中

  

另一方面,“singleInstance”活动不允许其他活动成为其任务的一部分。这是任务中唯一的活动。如果它启动另一个活动,则该活动将分配给另一个任务

答案 1 :(得分:0)

我做了快速解决,以避免内存泄漏

//where masterAct is static variable to MasterView.this
//and androAct is static variable define in MyActivity class as MyActivity.this

                                            if (MyActivity.androAct != null){
                                                MyActivity.androAct.finish();
                                            }


                                            //Intent intent = new Intent(masterAct, MyActivity.class);
                                            Intent intent = new Intent();
                                            intent.setClassName(
                                                    "org.mypackage",
                                                    "org.mypackage.ui.MyActivity");

                                                    intent.setAction("com.google.zxing.client.android.ENCODE");
                                                intent.putExtra("ENCODE_TYPE", "TEXT_TYPE");
                                                intent.putExtra ("ENCODE_DATA", "123456789");
                                                //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                                                masterAct.startActivity(intent);