简体   繁体   English

如何检查给定的 package 是否被禁用,然后相应地显示 toast 消息?

[英]How to check if a given package is disabled or not and then show toast message accordingly?

I'm making an app where it uses intent to send data to another app.我正在制作一个应用程序,它使用意图将数据发送到另一个应用程序。 In case, the other app, which is supposed to receive data from my app, is not installed on users's device then it redirects user to play store with toast message asking user to install it.万一,应该从我的应用程序接收数据的另一个应用程序未安装在用户的设备上,则它将用户重定向到播放商店,并带有 toast 消息要求用户安装它。 I used "if else" to achieve this.我使用“if else”来实现这一点。 It worked all good until I found that if the other app is disabled by user (OEM installed app which can't be uninstalled), then my app crashes.一切都很好,直到我发现如果其他应用程序被用户禁用(OEM 安装的应用程序无法卸载),那么我的应用程序就会崩溃。 In such a condition, I want to let user know that the app is disabled by them and ask them to enable it (through toast message).在这种情况下,我想让用户知道该应用程序已被他们禁用并要求他们启用它(通过 toast 消息)。 How can I achieve this?我怎样才能做到这一点?

Here is my complete code:这是我的完整代码:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        // Creates button view which is connected to a view in the XML layout, which gets triggered on touching the view.
        TextView textView = (TextView) findViewById(R.id.location);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // Use package name which we want to check
                boolean isAppInstalled = appInstalledOrNot("com.google.android.apps.maps");


                if(isAppInstalled){

                    Uri gmmIntentUri = Uri.parse("geo:00,0000,00,0000");
                    Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
                    mapIntent.setPackage("com.google.android.apps.maps");
                    startActivity(mapIntent);

                } else {
                    Uri uri2 = Uri.parse("market://details?id=com.google.android.apps.maps");
                    Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri2);
                    Toast.makeText(MainActivity.this, "Google Maps not Installed", Toast.LENGTH_SHORT).show();
                    startActivity(goToMarket);
                }
            }


        });
        
    }

    private boolean appInstalledOrNot(String uri) {
        PackageManager pm = getPackageManager();
        try {
            pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
        }

        return false;
    }


}

I'm not sure if this will work for you, but since you know the package name, you could try this to do a check beforehand.我不确定这是否适合您,但由于您知道 package 名称,您可以尝试这样做事先检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM