为Android软件创建快捷方式
16lz
2021-01-23
注意:需要添加权限<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
以下是全部代码:
package shortcut.demo;import android.app.Activity;import android.content.Intent;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.os.Bundle;import android.os.Parcelable;import android.preference.PreferenceManager;import android.widget.Toast;public class ShorcutDemoActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); makeShortCut(); } /** * 创建桌面快捷方式 */private void makeShortCut() {SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);Editor editor = sp.edit();boolean isFirstStart = sp.getBoolean("isFirstStart", true);if(!isFirstStart) return; Parcelable appIcon = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher); //获取快捷键的图标 String appName = getString(R.string.app_name); Intent addIntent=new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); Intent myIntent=new Intent(this, ShorcutDemoActivity.class); addIntent.putExtra("duplicate", false); //不允许重复创建 addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName);//快捷方式的标题 addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, appIcon);//快捷方式的图标 addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent);//快捷方式的动作 sendBroadcast(addIntent);//发送广播 editor.putBoolean("isFirstStart", false); editor.commit(); Toast.makeText(this, "成功创建”"+ appName +"“快捷方式", Toast.LENGTH_LONG).show();}}
更多相关文章
- android 创建快捷方式图标
- Android图标靠界面的右边的实现
- 修改android桌面图标默认大小
- Android Studio快捷键、配置 Android Studio、cmake配置Android
- android studio 快捷键描述和设置
- android快捷键
- Android多分辨率适配实践【1】使用字体图标(内含两枚神器)