Opening System Application Using Intent

↘锁芯ラ 提交于 2019-12-01 09:43:00

问题


I am trying to make a simple application which will send the user to a specific (system installed) app (system settings, calendar, browser, etc.) when clicked by the user from the home screen or app drawer.

For instance, I am currently trying to open the system settings whenever my app is launched, just like a shortcut for settings does.

Is it possible to implement this the way I want it? Does anyone have any suggestions?

Here is my code:

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;


public class MainActivity extends Activity {

    public void  LaunchComponent (String packageName, String name){
        Intent i = new Intent(Intent.ACTION_MAIN);
        PackageManager manager = getPackageManager();
        i = manager.getLaunchIntentForPackage("com.sec.android.app.controlpanel");
        i.addCategory(Intent.CATEGORY_LAUNCHER);
        startActivity(i);

    }
}

I currently do not have a layout file, such as main.xml, in my app, as it doesn't have any UI or layout elements. All it should do is send the user to another app activity. I have been using this tutorial (missing link) and I've implemented all the code examples and I cannot figure it out, I am new to android development and java is not my best language. Open to any criticism or suggestions.


回答1:


You have to make the call to LaunchComponent which can be done in onCreate first life-cycle callback function

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
     LaunchComponent (packageName, name);
}

updated

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;


public class MainActivity extends Activity {

public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
         LaunchComponent ("com.sec.android.app.controlpanel", "abc?");
    }


public void  LaunchComponent (String packageName, String name){
    Intent i = new Intent(Intent.ACTION_MAIN);
    PackageManager manager = getPackageManager();
    i = manager.getLaunchIntentForPackage(packageName);
    i.addCategory(Intent.CATEGORY_LAUNCHER);
    startActivity(i);

}



回答2:


In this example you can open system alarm clock app., hope it helps, example activity:

public class TestActivity extends Activity {

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

        PackageManager packageManager = this.getPackageManager();
        if (packageManager != null) {

            Intent AlarmClockIntent = 
                new Intent(Intent.ACTION_MAIN).addCategory(
                        Intent.CATEGORY_LAUNCHER).setComponent(
                                new ComponentName("com.android.deskclock", "com.android.deskclock.DeskClock"));

            ResolveInfo resolved = packageManager.resolveActivity(AlarmClockIntent, PackageManager.MATCH_DEFAULT_ONLY);
            if (resolved != null) {
                startActivity(AlarmClockIntent);
                finish();
                return;
            } else {
                // required activity can not be located!
            }
        }
    }
}


来源:https://stackoverflow.com/questions/11268060/opening-system-application-using-intent

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