下面列出了android.content.Context#sendBroadcast ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onReceive(Context context, Intent intent) {
String notificationId = intent.getStringExtra("_id");
String rrule = intent.getStringExtra("rrule");
long dstart = intent.getLongExtra("dstart",Calendar.getInstance().getTimeInMillis());
int index = intent.getIntExtra("index",-1);
String action = intent.getStringExtra("action");
try {
Intent tent = Intent.parseUri(action, 0);
context.startActivity(tent);
}catch (Exception e){
e.printStackTrace();
}
if(intent.getBooleanExtra("collapse",true)) {
Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
context.sendBroadcast(it);
}
if(intent.getBooleanExtra("dismiss",true)){
DeletePendingIntent.DeleteNotification(context,notificationId,rrule,dstart);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(Integer.parseInt(notificationId));
}
}
@Override
public void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException {
try {
Class miuiNotificationClass = Class.forName("android.app.MiuiNotification");
Object miuiNotification = miuiNotificationClass.newInstance();
Field field = miuiNotification.getClass().getDeclaredField("messageCount");
field.setAccessible(true);
field.set(miuiNotification, String.valueOf(badgeCount == 0 ? "" : badgeCount));
} catch (Exception e) {
Intent localIntent = new Intent(
INTENT_ACTION);
localIntent.putExtra(EXTRA_UPDATE_APP_COMPONENT_NAME, componentName.getPackageName() + "/" + componentName.getClassName());
localIntent.putExtra(EXTRA_UPDATE_APP_MSG_TEXT, String.valueOf(badgeCount == 0 ? "" : badgeCount));
if (BroadcastHelper.canResolveBroadcast(context, localIntent)) {
context.sendBroadcast(localIntent);
} else {
throw new ShortcutBadgeException("unable to resolve intent: " + localIntent.toString());
}
}
}
/**
* 创建快捷方式
*
* @param cxt
* Context
* @param icon
* 快捷方式图标
* @param title
* 快捷方式标题
* @param cls
* 要启动的类
*/
public void createDeskShortCut(Context cxt, int icon, String title,
Class<?> cls) {
// 创建快捷方式的Intent
Intent shortcutIntent = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT");
// 不允许重复创建
shortcutIntent.putExtra("duplicate", false);
// 需要现实的名称
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
// 快捷图片
Parcelable ico = Intent.ShortcutIconResource.fromContext(
cxt.getApplicationContext(), icon);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, ico);
Intent intent = new Intent(cxt, cls);
// 下面两个属性是为了当应用程序卸载时桌面上的快捷方式会删除
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
// 点击快捷图片,运行的程序主入口
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
// 发送广播。OK
cxt.sendBroadcast(shortcutIntent);
}
/**
* 让系统接受广播刷新图片库 能马上看到该图片
*/
public static void scanPhoto(Context ctx, String imgFileName) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File file = new File(imgFileName);
Uri contentUri = Uri.fromFile(file);
mediaScanIntent.setData(contentUri);
ctx.sendBroadcast(mediaScanIntent);
}
public void onReceive(Context paramContext, Intent intent) {
String type;
String state = intent.getAction();
switch (state) {
default:
type = LAST_FM_META_CHANGED;
break;
case "com.sonyericsson.music.playbackcontrol.ACTION_PAUSED":
type = LAST_FM_PLAYBACK_PAUSED;
break;
case "com.sonyericsson.music.TRACK_COMPLETED":
type = LAST_FM_PLAYBACK_COMPLETE;
break;
}
Bundle bundle = intent.getExtras();
if (bundle != null) {
Intent localIntent = new Intent(type);
localIntent.putExtra("artist",
bundle.getString("ARTIST_NAME"));
localIntent.putExtra("album",
bundle.getString("ALBUM_NAME"));
localIntent.putExtra("track",
bundle.getString("TRACK_NAME"));
localIntent.putExtra("playing",
!type.equals(LAST_FM_PLAYBACK_PAUSED));
localIntent.putExtra("duration",
bundle.getInt("TRACK_DURATION") / 1000);
localIntent.putExtra("source", "semc");
int i = bundle.getInt("TRACK_POSITION", -1);
if (i != -1) {
localIntent.putExtra("position", i / 1000);
}
paramContext.sendBroadcast(localIntent);
}
}
/**
* 更新系统图库
*
* @param context
* @param uri
*/
public static void UpdateMedia(Context context, Uri uri) {
if (uri == null || context == null) {
return;
}
Intent scannerIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
context.sendBroadcast(scannerIntent);
}
@Override
public void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException {
Intent intent = new Intent(INTENT_UPDATE_COUNTER);
intent.putExtra(PACKAGENAME, componentName.getPackageName());
intent.putExtra(CLASSNAME, componentName.getClassName());
intent.putExtra(COUNT, badgeCount);
if (BroadcastHelper.canResolveBroadcast(context, intent)) {
context.sendBroadcast(intent);
} else {
throw new ShortcutBadgeException("unable to resolve intent: " + intent.toString());
}
}
private static void sendBroadcast(Context context, String auctionInfo, byte[] imageBytes) {
String dataUrl = URL_BROADCAST_PREFIX + Uri.encode(auctionInfo);
Intent intent = new Intent(ACTION_BROADCAST, Uri.parse(dataUrl));
intent.putExtra(KEY_IMAGE, imageBytes);
context.sendBroadcast(intent);
}
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras == null)
return;
// To display mContext Toast whenever there is an SMS.
// Toast.makeText(mainScriptContext,"Recieved",Toast.LENGTH_LONG).show();
Object[] pdus = (Object[]) extras.get("pdus");
for (int i = 0; i < pdus.length; i++) {
SmsMessage SMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
String sender = SMessage.getOriginatingAddress();
String body = SMessage.getMessageBody();
// A custom Intent that will used as another Broadcast
Intent in = new Intent("SmsMessage.intent.MAIN").putExtra("get_msg", sender + ":" + body);
// You can place your check conditions here(on the SMS or the
// sender)
// and then send another broadcast
context.sendBroadcast(in);
// This is used to abort the broadcast and can be used to silently
// process incoming message and prevent it from further being
// broadcasted. Avoid this, as this is not the way to program an
// app.
// this.abortBroadcast();
}
}
/**
* 刷新图库
*
* @param path 要刷新的文件的绝对路径
*/
public void refresh(Context cxt, String path) {
String name = path.substring(path.lastIndexOf('/'));
try {
MediaStore.Images.Media.insertImage(cxt.getContentResolver(), path,
name, null);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 最后通知图库更新
cxt.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri
.parse("file://" + path)));
}
public static void stopProxyService(Context context) {
Intent intent = new Intent(context.getString(R.string.stop_service));
intent.setPackage(context.getPackageName());
context.sendBroadcast(intent);
}
/**
* <p>Broadcasts the given player state as the one being set.</p>
*/
public static void broadcastPlaybackStatusChange(Context context, MusicDirectory.Entry song, PlayerState state) {
try {
Intent intent = new Intent(EVENT_PLAYSTATE_CHANGED);
Intent avrcpIntent = new Intent(AVRCP_PLAYSTATE_CHANGED);
switch (state) {
case STARTED:
intent.putExtra("state", "play");
avrcpIntent.putExtra("playing", true);
break;
case STOPPED:
intent.putExtra("state", "stop");
avrcpIntent.putExtra("playing", false);
break;
case PAUSED:
intent.putExtra("state", "pause");
avrcpIntent.putExtra("playing", false);
break;
case PREPARED:
// Only send quick pause event for samsung devices, causes issues for others
if (Build.MANUFACTURER.toLowerCase().contains("samsung")) {
avrcpIntent.putExtra("playing", false);
} else {
return; // Don't broadcast anything
}
break;
case COMPLETED:
intent.putExtra("state", "complete");
avrcpIntent.putExtra("playing", false);
break;
default:
return; // No need to broadcast.
}
addTrackInfo(context, song, avrcpIntent);
if (state != PlayerState.PREPARED) {
context.sendBroadcast(intent);
}
context.sendBroadcast(avrcpIntent);
} catch (Exception e) {
Log.e(TAG, "Failed to broadcastPlaybackStatusChange", e);
}
}
@Override
protected void closeActPanel(Context context) {
Intent intent = new Intent();
intent.setAction(XpNavBarAction.ACT_CLOSE_ACT_PANEL);
context.sendBroadcast(intent);
}
public static void launchAction(final Context context, Intent intent) {
Intent launchIntent = new Intent(ACTION);
launchIntent.putExtras(intent.getExtras());
context.sendBroadcast(launchIntent);
}
public static void voiceNormal(Context context) {
Intent intent = new Intent(VideoLiveWallpaper.VIDEO_PARAMS_CONTROL_ACTION);
intent.putExtra(VideoLiveWallpaper.KEY_ACTION, VideoLiveWallpaper.ACTION_VOICE_NORMAL);
context.sendBroadcast(intent);
}
/**
* 通过广播的方式通知系统扫描某个文件
* @param appContext appContext
* @param filePath 要被扫描的文件路径
*/
public static void notifySysScanFile(Context appContext, String filePath) {
Uri data = Uri.parse("file://" + filePath);
Intent toScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, data);
appContext.sendBroadcast(toScanIntent);
}
public static void launchAction(final Context context, Intent intent) {
Intent launchIntent = new Intent(ACTION);
context.sendBroadcast(launchIntent);
}
/**
* @param context
*/
public static void notifyScanMediaFiles(Context context) {
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
}
public static void broadcastPlayClicked(Context context) {
Intent intent = new Intent(MyMediaBrowserService.PLAYER_EVENT);
intent.putExtra(MyMediaBrowserService.ACTION_TYPE, PlaybackState.ACTION_PLAY);
context.sendBroadcast(intent);
}
/**
* Send a message to the connected Pebble to close an application identified by a UUID. If this application is not
* currently running, the message is ignored.
*
* @param context
* The context used to send the broadcast.
* @param watchappUuid
* A UUID uniquely identifying the target application. UUIDs for the stock kit applications are available in
* {@link Constants}.
*
* @throws IllegalArgumentException
* Thrown if the specified UUID is invalid.
*/
public static void closeAppOnPebble(final Context context, final UUID watchappUuid)
throws IllegalArgumentException {
if (watchappUuid == null) {
throw new IllegalArgumentException("uuid cannot be null");
}
final Intent stopAppIntent = new Intent(INTENT_APP_STOP);
stopAppIntent.putExtra(APP_UUID, watchappUuid);
context.sendBroadcast(stopAppIntent);
}