从自己的应用程序调用第三方应用程序活动 [英] Call third party app Activity from own App

查看:104
本文介绍了从自己的应用程序调用第三方应用程序活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从我的应用程序成功调用了第三方应用程序



诸如Google云端硬盘和Google相册之类的应用程序



代码:

  btnL1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(视图){
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){
返回false;
//抛出新的PackageManager.NameNotFoundException();
}
i.addCategory(Intent.CATEGORY_LAUNCHER);
context.startActivity(i);
返回true;
} catch(异常e){
return false;
}
}

更新#1



屏幕截图2 (仅称为Hello World应用,然后按回退以退出Hello World)



下启动允许他们启动相同的任务。如果没有,则您的选择是:




  • 在启动第三方活动之前禁用任务锁定

  • 请第三方活动的维护者更改其启动模式

  • 找到其他第三方活动


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);
            }
        });
    }

解决方案

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

这篇关于从自己的应用程序调用第三方应用程序活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆