Android 开发之系统应用Launcher详解,简单添加和删除快捷方式及常见问题
是什么?
是系统启动后加载的第一个应用程序
是其他应用程序的入口
2.Launcher的构成:
:
应用程序的快捷方式
:桌面小部件,图形不规则
文件夹
ContentProvider的形式展示应用中特定数据的集合
壁纸
:
4. 通过按钮添加或者删除应用程序的快捷方式Demo:
:
Main.xml
[java]
view plain
copy
1. <?xml version="1.0" encoding="utf-8"?>
2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. "fill_parent"
4. "fill_parent"
5. "vertical" >
6.
7. <TextView
8. "fill_parent"
9. "wrap_content"
10. "@string/hello" />
11.
12. <Button
13. "@+id/BT_InstallShortCurt"
14. "match_parent"
15. "wrap_content"
16. "Install ShortCurt" />
17.
18. <Button
19. "@+id/BT_UnInstallShortCurt"
20. "match_parent"
21. "wrap_content"
22. "UnInstall ShortCurt" />
23.
24. </LinearLayout>
Mainifest.xml
[java]
view plain
copy
1. <?xml version="1.0" encoding="utf-8"?>
2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3. package="com.itheima.lancher"
4. "1"
5. "1.0" >
6.
7. "8" />
8. <!-- 该权限为系统自定义权限 -->
9. "com.android.launcher.permission.INSTALL_SHORTCUT" />
10. "com.android.launcher.permission.UNINSTALL_SHORTCUT" />
11. <application
12. "@drawable/ic_launcher"
13. "@string/app_name" >
14. <activity
15. ".LancherDemoActivity"
16. "@string/app_name" >
17. <intent-filter>
18. "android.intent.action.MAIN" />
19.
20. "android.intent.category.LAUNCHER" />
21. </intent-filter>
22. </activity>
23.
24. ".ShortCutActivity">
25. <intent-filter >
26. <!-拦截添加快捷方式,显示土司->
27. "android.intent.action.CREATE_SHORTCUT"/>
28. </intent-filter>
29. </activity>
30. </application>
31.
32. </manifest>
LancherDemoActivity;
[java]
view plain
copy
1. import android.app.Activity;
2. import android.content.ComponentName;
3. import android.content.Intent;
4. import android.os.Bundle;
5. import android.view.View;
6. import android.view.View.OnClickListener;
7. import android.widget.Button;
8.
9. public class LancherDemoActivity extends Activity {
10. private Button button1;
11. private Button button2;
12.
13. public void onCreate(Bundle savedInstanceState) {
14. super.onCreate(savedInstanceState);
15. setContentView(R.layout.main);
16. button1 = (Button) findViewById(R.id.BT_InstallShortCurt);
17. button2 = (Button) findViewById(R.id.BT_UnInstallShortCurt);
18. new OnClickListener() {
19.
20. public void onClick(View v) {
21. addShortCurt2DeskTop();
22. }
23.
24. /**
25. * 添加桌面快捷方式
26. */
27. private void addShortCurt2DeskTop() {
28. new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
29. //快捷方式的名称
30. "TonyAutoShortCut");
31. "duplicate", false); //不允许重复创建
32. this, R.drawable.beauty));
33. new Intent(LancherDemoActivity.this,LancherDemoActivity.class).setAction(<span style="color:#FF0000;">"com.android.action.test"</span>));
34.
35. //发送广播
36. sendBroadcast(shortcut);
37. }
38. });
39.
40. /**
41. * 删除桌面快捷方式
42. */
43. button2.setOnClickListener(new OnClickListener() {
44. public void onClick(View v) {
45. deleteShortCurt2DeskTop();
46. }
47.
48. private void deleteShortCurt2DeskTop() {
49. new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
50.
51. //快捷方式的名称
52. "TonyAutoShortCut");
53.
54. new Intent(LancherDemoActivity.this,LancherDemoActivity.class).setAction(<span style="color:#FF0000;">"com.android.action.test"</span>));
55. sendBroadcast(shortcut);
56. }
57. });
58.
59.
60. }
61. }
注意事项: 在设置 EXTRA_SHORTCUT_INTENT时,添加和删除快捷方式的这个INTENT对象的ACTION属性必须设置为相同内容,才能声明删除和创建的快捷方式是一个,进行绑定,
否则删除无法生效
是什么?
是系统启动后加载的第一个应用程序
是其他应用程序的入口
2.Launcher的构成:
:
应用程序的快捷方式
:桌面小部件,图形不规则
文件夹
ContentProvider的形式展示应用中特定数据的集合
壁纸
:
4. 通过按钮添加或者删除应用程序的快捷方式Demo:
:
Main.xml
[java]
view plain
copy
1. <?xml version="1.0" encoding="utf-8"?>
2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. "fill_parent"
4. "fill_parent"
5. "vertical" >
6.
7. <TextView
8. "fill_parent"
9. "wrap_content"
10. "@string/hello" />
11.
12. <Button
13. "@+id/BT_InstallShortCurt"
14. "match_parent"
15. "wrap_content"
16. "Install ShortCurt" />
17.
18. <Button
19. "@+id/BT_UnInstallShortCurt"
20. "match_parent"
21. "wrap_content"
22. "UnInstall ShortCurt" />
23.
24. </LinearLayout>
Mainifest.xml
[java]
view plain
copy
1. <?xml version="1.0" encoding="utf-8"?>
2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3. package="com.itheima.lancher"
4. "1"
5. "1.0" >
6.
7. "8" />
8. <!-- 该权限为系统自定义权限 -->
9. "com.android.launcher.permission.INSTALL_SHORTCUT" />
10. "com.android.launcher.permission.UNINSTALL_SHORTCUT" />
11. <application
12. "@drawable/ic_launcher"
13. "@string/app_name" >
14. <activity
15. ".LancherDemoActivity"
16. "@string/app_name" >
17. <intent-filter>
18. "android.intent.action.MAIN" />
19.
20. "android.intent.category.LAUNCHER" />
21. </intent-filter>
22. </activity>
23.
24. ".ShortCutActivity">
25. <intent-filter >
26. <!-拦截添加快捷方式,显示土司->
27. "android.intent.action.CREATE_SHORTCUT"/>
28. </intent-filter>
29. </activity>
30. </application>
31.
32. </manifest>
LancherDemoActivity;
[java]
view plain
copy
1. import android.app.Activity;
2. import android.content.ComponentName;
3. import android.content.Intent;
4. import android.os.Bundle;
5. import android.view.View;
6. import android.view.View.OnClickListener;
7. import android.widget.Button;
8.
9. public class LancherDemoActivity extends Activity {
10. private Button button1;
11. private Button button2;
12.
13. public void onCreate(Bundle savedInstanceState) {
14. super.onCreate(savedInstanceState);
15. setContentView(R.layout.main);
16. button1 = (Button) findViewById(R.id.BT_InstallShortCurt);
17. button2 = (Button) findViewById(R.id.BT_UnInstallShortCurt);
18. new OnClickListener() {
19.
20. public void onClick(View v) {
21. addShortCurt2DeskTop();
22. }
23.
24. /**
25. * 添加桌面快捷方式
26. */
27. private void addShortCurt2DeskTop() {
28. new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
29. //快捷方式的名称
30. "TonyAutoShortCut");
31. "duplicate", false); //不允许重复创建
32. this, R.drawable.beauty));
33. new Intent(LancherDemoActivity.this,LancherDemoActivity.class).setAction(<span style="color:#FF0000;">"com.android.action.test"</span>));
34.
35. //发送广播
36. sendBroadcast(shortcut);
37. }
38. });
39.
40. /**
41. * 删除桌面快捷方式
42. */
43. button2.setOnClickListener(new OnClickListener() {
44. public void onClick(View v) {
45. deleteShortCurt2DeskTop();
46. }
47.
48. private void deleteShortCurt2DeskTop() {
49. new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
50.
51. //快捷方式的名称
52. "TonyAutoShortCut");
53.
54. new Intent(LancherDemoActivity.this,LancherDemoActivity.class).setAction(<span style="color:#FF0000;">"com.android.action.test"</span>));
55. sendBroadcast(shortcut);
56. }
57. });
58.
59.
60. }
61. }
注意事项: 在设置 EXTRA_SHORTCUT_INTENT时,添加和删除快捷方式的这个INTENT对象的ACTION属性必须设置为相同内容,才能声明删除和创建的快捷方式是一个,进行绑定,
否则删除无法生效