<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">在手机中运行App时,很奇怪的问题,有的手机运行App后桌面有快捷图标,有的手机没有快捷图标,</span>
经过在网上找了的资料,自己实践了一下,测试也没有问题,现在把步骤与代码贴出来给大家盾一下。
刚接触Android开发,好多代码都是Copy过来的,每行代码只能通过字面意思理解。
AndroidManifest.xml文件中添加以下权限设置,还有设置activity的intent-filter节点.
这个应该是必须要添加的,后面两个我也带上怕出什么问题。
"com.android.launcher.permission.INSTALL_SHORTCUT"
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_REPLACED" />
<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/appicon"
android:label="@string/app_name"
android:theme="@style/AppBaseTheme" >
<activity
android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:logo="@drawable/appicon">
</activity>
在我的启动SplashActivity中实现以下代码:
/**For shortcut**/
private Context mContext=SplashActivity.this;
private SharedPreferences appPreferences;
private boolean isAppInstalled=false;
private final long SPLASH_LENGTH=1000;
private Handler handler=new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
/*
//hide android status bar.
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//hide title bar-title bar of current activity
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
*/
setContentView(R.layout.activity_splash);
/******For Shortcut*******/
appPreferences=PreferenceManager.getDefaultSharedPreferences(mContext);
isAppInstalled=appPreferences.getBoolean("isAppInstalled", false);
if(isAppInstalled==false)
{
addShortcutIcon();
}
SharedPreferences.Editor editor=appPreferences.edit();
editor.putBoolean("isAppInstalled", true);
editor.commit();
/*************/
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(SPLASH_LENGTH);
} catch (Exception e) {
// TODO: handle exception
}
runOnUiThread(new Runnable() {
@Override
public void run() {
startActivity(new Intent(SplashActivity.this,MainActivity.class));
finish();
}
});
}
}).start();
/*handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Intent intent=new Intent(SplashActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
}, SPLASH_LENGTH);
*/
}
添加桌面快捷图标方法:
private void addShortcutIcon() {
Intent shortcutIntent=new Intent(getApplicationContext(),SplashActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent=new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getApplicationContext()
,R.drawable.appicon));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
//finally broadcast the new intent
getApplicationContext().sendBroadcast(addIntent);
}