通过Activity广播建立桌面快捷方式,并监听快捷方式实现。
1,广播发送者
public class UrgentCall extends Activity implements OnClickListener {
Button police;
Button fire;
Intent directCall;
//通知广播接收识别
private final String ACTION_ADD_SHORTCUT =
"com.android.launcher.action.INSTALL_SHORTCUT";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
police = (Button)findViewById(R.id.police);
fire = (Button)findViewById(R.id.firepolice);
police.setOnClickListener(this);
fire.setOnClickListener(this);
//传递一个电话Intent动作
directCall = new Intent(Intent.ACTION_CALL);
}
public void onClick(View v) {
//快捷方式的动作(Intent)
Intent addShortcut = new Intent(ACTION_ADD_SHORTCUT);
String numToDial = null;
//图片对象
Parcelable icon = null;
switch (v.getId()) {
case R.id.police:
numToDial = "110";
//获取图片
icon = Intent.ShortcutIconResource.fromContext(this,R.drawable.jing);
break;
case R.id.firepolice:
numToDial = "119";
//获取图片
icon = Intent.ShortcutIconResource.fromContext(this,R.drawable.huo);
break;
default:
break;
}
//加入快捷方式的name
addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, numToDial);
//tel://119
directCall.setData(Uri.parse("tel://"+numToDial));
//加入快捷方式的附加Intent(快捷方式将要进行的Intent)
addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, directCall);
//加入快捷方式的图标
addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
//发送广播(建立快捷方式的广播)
sendBroadcast(addShortcut);
}
2.快捷监听者
public class FireShortcut extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent addShortcut;
//判断获取Intent的动作是否是广播发送者传递的
if (getIntent().getAction() .equals(Intent.ACTION_CREATE_SHORTCUT)) {
/*初始化添加快捷图标的Intent*/
addShortcut = new Intent();
addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "119");
//ShortcutIconResource用来构造快捷方式的图标
Parcelable icon = Intent.ShortcutIconResource.fromContext(this,R.drawable.huo);
addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
//建立一个Intent的电话动作
Intent callFirePolice = new Intent(Intent.ACTION_CALL,Uri.parse("tel://119"));
addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,callFirePolice);
/*设置Result*/
setResult(RESULT_OK,addShortcut);
} else {
setResult(RESULT_CANCELED);
}
finish();
}
}

浙公网安备 33010602011771号