簡體   English   中英

將應用程序添加到主屏幕快捷方式

[英]Add app to home screen shortcut

我認為那個標題說明了一切。 我只想在主屏幕上創建快捷方式,因為我看到越來越多的應用在安裝后將其快捷方式放置在主屏幕上。 我嘗試了一些代碼,但不起作用。 有我的活動和清單文件:

我的活動

import com.files.getquote.R;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;


public class GetQuoteActivity extends Activity {

    WebView myBrowser;
;
    /** Called when the activity is first created. */
    @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        myBrowser = (WebView)findViewById(R.id.mybrowser);

        myBrowser.loadUrl("file:///android_asset/index.html"); 

        myBrowser.getSettings().setDatabasePath("/data/data/" + myBrowser.getContext().getPackageName() + "/databases/");

        myBrowser.getSettings().setDomStorageEnabled(true);

        myBrowser.getSettings().setJavaScriptEnabled(true);

        myBrowser.getSettings().setDatabaseEnabled(true);


    }

}

AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.files.getquote"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />
<uses-permission
        android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
    <application android:icon="@drawable/ic_launcher"  android:label="@string/app_name">
        <activity android:name=".GetQuoteActivity" android:theme="@android:style/Theme.NoTitleBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

為了在Android的主屏幕上添加快捷方式,您需要執行以下操作:

表現

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

在應用程序中的某個位置(最好在應用程序啟動時以一種可以調用的方法放置?)

Intent shortcut = new Intent(getApplicationContext(), MainActivity.class);
shortcut.setAction(Intent.ACTION_MAIN);

Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcut);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "YOUR APP SHORTCUT TEXT");
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getApplicationContext(),                                         
    R.drawable.ic_launcher));
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(intent);

您將需要一個接收器來接受廣播。 但這應該為您指明正確的方向。 您還需要指定android:exported="true" -想要從清單中的快捷方式啟動應用程序時。 讓我猜了幾個小時! :)

並為該主題提供更多說明: 將android應用程序的快捷方式添加到主屏幕在按鈕上單擊

您可以使用它來創建快捷方式:

Intent shortcutIntent = new Intent (this, YourActivity.class);      
Intent addIntent = new Intent(); 
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Title"); 
addIntent.putExtra("duplicate", false); 
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon)); 
addIntent.   
sendBroadcast(addIntent);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM