Call third party app Activity from own App

隐身守侯 提交于 2019-12-25 08:42:50

问题


I have successfully called third party apps from my app

Apps like: Google Drive and Google Photos

Code:

btnL1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
     openApp(KioskActivity.this, "com.google.android.apps.docs");
   }
});

btnL2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
     openApp(KioskActivity.this, "com.google.android.apps.photos");
    }
});


public static boolean openApp(Context context, String packageName) {
        PackageManager manager = context.getPackageManager();
        try {
            Intent i = manager.getLaunchIntentForPackage(packageName);
            if (i == null) {
                return false;
                //throw new PackageManager.NameNotFoundException();
            }
            i.addCategory(Intent.CATEGORY_LAUNCHER);
            context.startActivity(i);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

UPDATE # 1 Here is the Code I have followed to call third party app Activity from own App

On button click calling Activity of third party app (Like: Last FM)

final Intent intentDeviceTest = new Intent("android.intent.action.MAIN");                
intentDeviceTest.setComponent(new ComponentName("fm.last.android","fm.last.android.LastFm"));
startActivity(intentDeviceTest);

But always getting android.content.ActivityNotFoundException: Unable to find explicit activity class {fm.last.android/fm.last.android.LastFm}; have you declared this activity in your AndroidManifest.xml? And If I use try-catch blog or if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } to avoid ANF exception then its not showing exception, but still unable to call an activity

I already installed Last FM app on my device, so what could be the reason ?

UPDATE # 2 : I have created Hello World app and successfully called that one

Screenshots

Screenshot 1 (just enabled Kiosk Mode)

Screenshot 2 (just called Hello World app and pressed back to exit Hello World)

Question: Why it showing Navigation Bar and Bottom Bar (I mean Back, Home and Recents keys)

Here is my updated code:

  public class KioskActivity extends Activity {        

    final int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().getDecorView().setSystemUiVisibility(flags);

        getActionBar().hide();

        setContentView(wenchao.kiosk.R.layout.activity_lock_activity);

        DevicePolicyManager myDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);

        ComponentName mDPM = new ComponentName(this, MyAdmin.class);

        if (myDevicePolicyManager.isDeviceOwnerApp(this.getPackageName())) {

            String[] packages = {this.getPackageName()};
            startLockTask();
        } else {
            Toast.makeText(getApplicationContext(),"Not owner", Toast.LENGTH_LONG).show();
        }

        setVolumMax();

        Button lock_btn = (Button)findViewById(wenchao.kiosk.R.id.lock_button);
        Button unlock_btn = (Button)findViewById(wenchao.kiosk.R.id.unlock_button);
        Button appButton = (Button) findViewById(R.id.appButton);

        lock_btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                startLockTask();
                return false;
            }
        });

        unlock_btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                stopLockTask();
                return false;
            }
        });

        appButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final Intent intentDeviceTest = new Intent("android.intent.action.MAIN");
                intentDeviceTest.setComponent(new ComponentName("com.example.hello1","com.example.hello1.MainActivity"));
                startActivity(intentDeviceTest);
            }
        });
    }

回答1:


Kiosk mode is officially called task locking. The name contains a clue. You can only launch third party activities if their launch mode allows them to be launched into the same task. If they don't, your options are:

  • disable task locking before launching the third-party activity
  • petition the maintainer of the third-party activity to change its launch mode
  • find an alternative third-party activity


来源:https://stackoverflow.com/questions/36026860/call-third-party-app-activity-from-own-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!