首页 > 代码库 > 打开第三方应用

打开第三方应用

记录一下,自己用过的打开第三方应用的两种方法;


1.//根据包名类名启动第三方应用(要启动的应用的包名,要启动的activity)

openApp("com.xx.test", "com.xx.test.TestActivity");

   private void openApp(String pname,String aname){
//      changeInputSource(pname);
        ComponentName componentName = new ComponentName(pname,
                aname);
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setComponent(componentName);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        try {
            mContext.startActivity(intent);
        } catch (ActivityNotFoundException anf) {
            Toast.makeText(mContext, "找不到应用数据", Toast.LENGTH_SHORT).show();
        }
    }
2.//根据Action启动应用

startApp("com.xx.test.action");

	private void startApp(String action){
	    Intent pointIntent = new Intent(action);
        pointIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        try {
            mcontext.startActivity(pointIntent);
        } catch (ActivityNotFoundException anf) {
            Toast.makeText(mcontext, R.string.notfind, Toast.LENGTH_SHORT).show();
        }               //R.string.notfind = "找不到应用数据"

	}