将应用添加到主屏幕快捷方式

时间:2014-02-20 15:44:27

标签: android

我认为标题说明了一切。我只希望在主屏幕上创建快捷方式,因为我看到越来越多的应用程序在安装后将其快捷方式放在主屏幕上。我尝试了一些代码,但没有用。有我的活动和清单文件:

我的活动

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>

2 个答案:

答案 0 :(得分:4)

要在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" - 当您希望从清单中的快捷方式启动应用时。让我猜几个小时! :)

有关该主题的额外说明:Add Shortcut for android application To home screen On button click

答案 1 :(得分:0)

您可以使用它来创建快捷方式:

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);
相关问题