便捷始终是apps的追求之一,与PC终端一样,智能机安卓终端的桌面也是很重要的,为你的程序apps设置一个桌面快捷方式可以为你的apps增色不少。
首先我们有必要了解一下安卓手机系统的桌面,桌面包含壁纸和快捷方式:
1 壁纸:
安卓是通过WallPaperManager来管理手机桌面壁纸的,我们可以通过这个来开发实时壁纸。
2 快捷方式:
在程序中把一个软件的快捷方式添加到桌面上,只需要三步即可:
》创建一个添加快捷方式的Intent,该Intent的Action属性应该为com.android.launcher.action.INSTALL_SHORTCUT;
》通过为该Intent添加Extra属性来设置快捷方式的标题,图标及快捷方式对应启动的程序;
》调用sendBroadcast()方法发送广播即可添加快捷方式。
另外呢,别忘了在AndoidMainfest中加入权限配置:
<uses-permission andriod:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
示例代码如下:
public class AddShortcut extends Activity{
ImageView flower;
Animation anim,reverse;
final Handle handle=new Handle(){
@Override
public void handleMessage(Message msg){
if(msg.ehat==0x213){flower.startAnimation(reverse);
}
};
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanState);
setContentView(R.layout.main);
flower=(ImageView)findViewById(R.id.flower);
anim.setFillAfter(true);
reverse=AnimationUtils.loadAnimation(this,R.anim.reverse);
reverse.setFillAfter(true);
Button bn=(Button)findViewById(R.id.bn);
bn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View source){
Intent addIntent=new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
String title=getResources().getString(R.string.tilte);
Pacelable icon=Intent.ShortcutIconResource.fromContext(AddShortcut.this,R.drawabl.icon);
Intent myIntent=new Intent(AddShortcut.this,AddShortcut.class);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,title);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,myIntent);
sendBroadCast(addIntent);
}
});
}
@Override
public void onResume(){
super.onResume();
}
}
这就是我们通常在桌面上添加快捷方式,但是这样的话,只有程序被运行之后,才会生成快捷方式。另外,我们还可以在Launcher中添加快捷方式:
这种卡快捷方式是在系统菜单项中,添加这种快捷方式非常简便:只要在AndroidManifest配置文件中对Activity添加Intent配置:
例如:
<activity
android:name=".Addshortcut">
<intent-filter>
<action android:name="android.intent.action.MAIN">
<category android:name="android.intent.category.LAUNCHER">
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.CRETE_SHORTCUT"/>
</intent-filter>
</activity>