2014-02-20 93 views
0

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

我的活动

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> 

回答

3

为了一个快捷方式添加到主屏幕在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

+0

谢谢,但你能解释一下,或给例如vhere我应该把代码意图捷径? –

+0

你想解释一下什么?我以为我很清楚。基本上。把它放在一个方法中。调用类似于:createShortcut()。然后(测试)你的主要活动。坚持一个调用createShortcut()并检查你可以创建一个。那么你将不得不决定放置这个地方的最佳位置。正如我所说你会需要一个接收器来捕捉它。如果你需要接收器的帮助:http://developer.android.com/reference/android/content/BroadcastReceiver.html - 有你想要的所有信息:) – LokiSinclair

+0

@LokiSinclair ..谢谢你的解决方案。这对我来说可以。但我有另一个问题。我的应用支持多种语言。因此,当我在设置中更改手机语言时,我的应用名称在启动器中得到更新。但是应用程序名称不会更新为主屏幕中的新语言。我是否需要为此添加更多内容?感谢您的阅读.. – Sushil

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

谢谢你的解决方案。这对我来说可以。但我有另一个问题。我的应用支持多种语言。因此,当我在设置中更改手机语言时,我的应用名称在启动器中得到更新。但是应用程序名称不会更新为主屏幕中的新语言。我是否需要为此添加更多内容?谢谢阅读.. – Sushil

相关问题