添加快捷方式和删除快捷方式:
private void addShortcut() {
Intent shortcut = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT");
// 快捷方式的名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
getString(R.string.app_name));
shortcut.putExtra("duplicate", false); // 不同意反复创建
// 指定当前的Activity为快捷方式启动的对象
ComponentName comp = new ComponentName(this.getPackageName(),
getClass().getName());
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(
Intent.ACTION_MAIN).setComponent(comp));
// 快捷方式的图标
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(
this, R.drawable.icon);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
sendBroadcast(shortcut);
}
/**
* 删除程序的快捷方式。
*/ private void deleteShortcuts() { Intent shortcut = new Intent( "com.android.launcher.action.UNINSTALL_SHORTCUT"); // 快捷方式的名称 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name)); // 指定当前的Activity为快捷方式启动的对象 ComponentName comp = new ComponentName(this.getPackageName(), getClass().getName()); shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent( Intent.ACTION_MAIN).setComponent(comp)); sendBroadcast(shortcut); }
发邮件:
public boolean sendEmail(String to[], String subject, String body,
String attachementFilePath) {
final Intent emailIntent = new Intent(
android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, to);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
if (attachementFilePath != null) {
Uri attachmentUri = null;
try {
File file = new File(attachementFilePath);
if (file == null) {
Log.d("[RC] Mail", "File error: " + attachementFilePath);
} else if (!file.exists()) {
Log.d("[RC] Mail", "File does not exist: "
+ attachementFilePath);
} else if (!file.canRead()) {
Log.d("[RC] Mail", "File can't be read: "
+ attachementFilePath);
} else if (!file.isFile()) {
Log.d("[RC] Mail", "Invalid file: " + attachementFilePath);
} else {
attachmentUri = Uri.fromFile(file);
Log.d("[RC] Mail", "Attachement path[size=" + file.length()
+ "]: " + attachementFilePath);
Log.d("[RC] Mail",
"Attachement URI: " + attachmentUri.toString());
}
} catch (java.lang.Throwable ex) {
Log.e("[RC] Mail", "Error: " + ex.toString());
}
if (attachmentUri != null) {
emailIntent.putExtra(Intent.EXTRA_STREAM, attachmentUri);
}
}
emailIntent.setType(PLAIN_TEXT);
List<ResolveInfo> availableSoft = (List<ResolveInfo>) mContext
.getPackageManager().queryIntentActivities(emailIntent,
PackageManager.MATCH_DEFAULT_ONLY);
if (availableSoft.size() <= 0) {
return false;
}
mContext.startActivity(Intent.createChooser(emailIntent, mContext
.getResources().getString(R.string.menu_sendEmail)));
return true;
}
默认使用Google chrome打开WebView:
//new Intent(Intent.ACTION_VIEW, uri)
public void startActiviyByChromeIfExists(Context context,
Intent intent) {
try {
Log.d("startActiviyByChromeIfExists",
"Intent Scheme: " + intent.getScheme());
} catch (Exception e) {
}
if (context != null && intent != null) {
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
List<ResolveInfo> availableSoft = (List<ResolveInfo>) context
.getPackageManager().queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo info : availableSoft) {
if ("com.android.chrome".equals(info.activityInfo.packageName)) {
intent.setComponent(new ComponentName(
info.activityInfo.packageName,
info.activityInfo.name));
context.startActivity(intent);
return;
}
}
if (availableSoft.size() == 0) {
try {
Toast.makeText(mContext, R.string.setting_no_browser_installed, Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.e("startActiviyByChromeIfExists", e.getMessage());
}
} else {
context.startActivity(intent);
}
}
}
声明权限:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
关机广播:
<receiver android:name=".ShutdownReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_SHUTDOWN"/>
</intent-filter>
</receiver>
接受开机广播:
<receiver android:name="BootBroadcast" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver>