在Android中长按待机界面会出现添加快捷方式,窗口小部件等。现在把添加快捷方式的方法记录下来。窗口小部件的开发以后有时间在写下来。

本程序只有一个界面。程序加载进来后就做了一件事--添加快捷方式。退出程序后再待机界面上长按添加快捷方式,会看到程序的图标,点击就可以添加快捷方式了。

Activity如下:

package com.xiaochun91103; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Parcelable; public class ShortcutsTest extends Activity { /** Called when the activity is first created. */ private static final String EXTRA_KEY = "com.xiaochun91103.ShortcutsTest"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final Intent intent = getIntent(); String action = intent.getAction(); if(intent.ACTION_CREATE_SHORTCUT.equals(action)) { setupShortcut(); finish(); return; } } private void setupShortcut() { // TODO Auto-generated method stub Intent shortcutIntent = new Intent(Intent.ACTION_MAIN); shortcutIntent.setClassName(this, this.getClass().getName()); shortcutIntent.putExtra(EXTRA_KEY, "A Shortcuts Demo for Test"); Intent intent = new Intent(); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "测试快捷方式"); Parcelable iconResource = Intent.ShortcutIconResource.fromContext( this, R.drawable.shortcuts); intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource); setResult(RESULT_OK, intent); } }

在AndroidMenifest下需要做如下声明程序才能正常运行:

<activity android:name=".ShortcutsTest" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity-alias android:name=".ShortcutsTest" android:targetActivity=".ShortcutsTest" android:label="Shortcuts"> <!-- This intent-filter allows your shortcuts to be created in the launcher. --> <intent-filter> <action android:name="android.intent.action.CREATE_SHORTCUT" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity-alias>

程序截图如下:

更多相关文章

  1. ubuntu用wifi连接android调试程序的步骤
  2. Android(安卓)M WRITE_SETTINGS权限的一个BUG
  3. Android(安卓)7.0 Settings Summary 小记
  4. Android(安卓)7.0新特性概览
  5. android艺术开发探索之RemoteViews跨进程更新UI
  6. android悬浮按钮(Floating action button)的两种实现方法
  7. 阿福强烈推荐一个不错的技术网站,android程序猿们幸福的开始
  8. 【Android】18.1 利用安卓内置的定位服务实现位置跟踪
  9. Android之——流量管理程序示例

随机推荐

  1. android中使用AsyncTask做下载进度条
  2. android log日志文件输出
  3. Android Studio获取SHA1证书方法
  4. android自定义支持横竖方向切换seekbar控
  5. Get the Android SDK翻译
  6. Android开发窗口实现半透明效果
  7. Android内存控制
  8. android sqlite批量插入数据速度解决方案
  9. Cocos2d-x for Android(安卓)& iOS开发环
  10. Android(安卓)中使用EditText 点击全选再