简体   繁体   中英

Open the dialer app on the main screen

How can I open the diler app to the main screen which showes the recent and the search (not the dial pad).

I've tried context.getPackageManager().getLaunchIntentForPackage("com.android.diler") but it returns null the com.android.diler also return null.

How about this:

Intent intent = new Intent(Intent.ACTION_DIAL);
startActivity(intent);

EDIT: Providing code to launch the dialer as if from the HOME screen

Ah. The problem is that there isn't only one dialer. Each phone manufacterer can (and usually does) provide its own dialer application. So you would need to know the package name of the dialer application. Here's a way to figure that out:

    // Ask the PackageManager to return a list of Activities that support ACTION_DIAL
    PackageManager pm = getPackageManager();
    Intent intent = new Intent(Intent.ACTION_DIAL);
    List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
    List<String> packageList = new ArrayList<String>();
    if (list != null) {
        // For each entry in the returned list, get the package name and add that to a list (ignore duplicates)
        for (ResolveInfo r : list) {
            String packageName = r.activityInfo.packageName;
            if (!packageList.contains(packageName)) {
                packageList.add(packageName);
            }
        }
    }
    // Get a launch Intent for each package in the list
    final List<Intent> launchIntents = new ArrayList<Intent>();
    for (String p : packageList) {
        intent = pm.getLaunchIntentForPackage(p);
        if (intent != null) {
            launchIntents.add(intent);
        }
    }
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            if (launchIntents.size() > 0) {
                // Get the first launch Intent. If there are more than 1, we don't know how to choose!
                Intent intent = launchIntents.get(0);
                startActivity(intent);
            } else {
                // Couldn't find an way to launch the dialer
            }
        }
    });

You will still have a problem if the user has multiple apps installed that can respond to the DIAL action. You need to figure out a way of choosing the correct one. I'll leave that as an exercise for the reader.

Thanks for the question, it gave me an opportunity to have some fun finding a solution for you :-D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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