安装apk后创建快捷方式

时间:2020-04-23 05:39:54

标签: java android installation apk shortcut

如何添加一个简单的快捷方式,我整天都在搜索一段代码来工作,但所有内容都与高级快捷方式有关,而对于简单的快捷方式则一无所知

public class MainActivity extends AppCompatActivity {

    private void addShourcut(){
        Intent shortCutIntent = new Intent(getApplicationContext() ,MainActivity.class);

        shortCutIntent.setAction(Intent.ACTION_MAIN);

        Intent addIntent = new Intent();

        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT , shortCutIntent);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME , "TASTE live");
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE ,
        addIntent.ShortcutIconResource.fromContext(getApplicationContext() , R.mipmap.ic_launcher));
        addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        addIntent.putExtra("duplicate" , false);
        getApplicationContext().sendBroadcast(addIntent);

    }

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


        webView = findViewById(R.id.webView);
        webView.loadUrl("https://taste-airsoft.ro");
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setBuiltInZoomControls(false);
    }

}

我也尝试过此操作,但没有成功,我猜代码中缺少“ private void addShourcut()”,但我没有在何处添加它的想法,它将与“受保护的void”冲突

public class MainActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (BuildConfig.DEBUG) {
        final Intent shortcutIntent = new Intent(this, MainActivity.class);

        final Intent intent = new Intent();
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_launcher));
        intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        sendBroadcast(intent);
    }


    setContentView(R.layout.activity_main);

    webView = findViewById(R.id.webView);
    webView.loadUrl("https://taste-airsoft.ro");
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setBuiltInZoomControls(false);
}

}

有人可以构造这种结构,因为我可能会从我的错误中学到东西吗? 以XML授予的权限

0 个答案:

没有答案