【问题标题】:Add app to home screen shortcut将应用程序添加到主屏幕快捷方式
【发布时间】:2014-02-20 15:44:27
【问题描述】:

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

我的活动

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


    【解决方案1】:

    为了在 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

    【讨论】:

    • 谢谢,但你能解释一下或举个例子吗?我应该把代码意图快捷方式放在哪里?
    • 你想解释什么?我以为我很清楚。基本上。把它放在一个方法中。调用类似:createShortcut()。然后(测试)你的主要活动。拨打 createShortcut() 并检查您是否可以首先创建一个。然后你必须决定把它放在哪里最好。正如我所说,你需要一个接收器来捕捉它。如果您需要接收者方面的帮助:developer.android.com/reference/android/content/… - 拥有您想要的所有信息 :)
    • @LokiSinclair .. 感谢您的解决方案。这对我来说可以。但我还有另一个问题。我的应用程序支持多种语言。因此,当我在设置中更改手机语言时,我的应用名称会在启动器中正常更新。但应用程序名称不会在主屏幕中更新为新语言。我需要为此添加更多内容吗?感谢阅读..
    • 在那种情况下,我会删除您创建的快捷方式并在事后创建另一个快捷方式。一旦创建,我不知道有什么方法可以更新它。但是,其他人可能很清楚,干杯!
    【解决方案2】:

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

    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);
    

    【讨论】:

    • 感谢您的解决方案。这对我来说可以。但我还有另一个问题。我的应用程序支持多种语言。因此,当我在设置中更改手机语言时,我的应用名称会在启动器中正常更新。但应用程序名称不会在主屏幕中更新为新语言。我需要为此添加更多内容吗?感谢阅读..
    猜你喜欢
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多