android桌面长按增加快捷方式

两种快捷创建方式:
* 1,长按桌面选择或者小部件拖动
* 2,在本app创建


快捷方式和Launcher的有关。可以看看Launcher里对快捷方式的支持
1,增加图标到长按快捷方式列表,点击快捷方式要启动的activity
eg:
  <activity
android:name="com.example.shortcutdemo.LauncherShortcuts"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar" >(为了启动activity时看不到界面,只运行创建快捷方式的方法)
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
</intent-filter>
</activity>


在activity的intent-filter增加action:CREATE_SHORTCUT
点击快捷方式后的伪activiy,来完成动画,像金山的快捷方式
  
<activity
android:name=".activities.ActShortcutOneKey"
android:taskAffinity="com.hiapk.kbatterydoctor.shortonkey" <!-- 这个属性很重要,为了防止home键后,打开此activity时,同时打开和他一个task的activity-->
android:excludeFromRecents="true"
android:theme="@android:style/Theme.Translucent.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

2,上面activity的java的操作:在桌面创建快捷方式
public class LauncherShortcuts extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (Intent.ACTION_CREATE_SHORTCUT.equals(getIntent().getAction())) {

createShortCut();
} else {
setResult(RESULT_CANCELED);

}
finish();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

public void createShortCut() {
// 点击快捷方式前往的intent
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
// shortcutIntent.setClassName(this, this.getClass().getName());
shortcutIntent.setClass(this, OneKeyActivity.class);//-------OneKeyActivity.java是下面的伪动画界面。像金山电池的快速清理快捷方式。

// 封装到新的intent,给Launcher
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
getString(R.string.app_name));
Parcelable iconResource = Intent.ShortcutIconResource.fromContext(this,
R.drawable.ic_launcher);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);

setResult(RESULT_OK, intent);
//这里如果是本app要创建的话,sendBroadcast(intent);


}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值