AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.iaiai.activity" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="IaiaiActivity" android:label="@string/contact_title">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<!-- 添加快捷键权限 -->
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
</manifest>
main.xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/contactItems"
android:paddingBottom="5dip" android:paddingLeft="12dip"
android:paddingTop="5dip" android:paddingRight="12dip">
<Button android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/btn_create"
android:text="@string/create"></Button>
</LinearLayout>
IaiaiActivity.java类:
package com.iaiai.activity;标签:IaiaiActivity,应用程序,id,Intent,import,快捷方式,Android,android From: https://blog.51cto.com/u_15955464/6040281
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class IaiaiActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.btn_create);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent addIntent = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT");
Parcelable icon = Intent.ShortcutIconResource.fromContext(
IaiaiActivity.this, R.drawable.icon); // 获取快捷键的图标
Intent myIntent = new Intent(IaiaiActivity.this,
IaiaiActivity.class);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "快捷方式");// 快捷方式的标题
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);// 快捷方式的图标
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent);// 快捷方式的动作
sendBroadcast(addIntent);// 发送广播
}
});
}
}