Hibernate实战_笔记18(基于注解的元数据)
转载请注明出处,谢谢!
最近入了一部Neuxs 5,昨天因为开会的时候手机经常收到短信,想静音按音量键关的确是媒体音量!在通知栏里也找不到快速切换按钮。于是萌生了自己写一个桌面widget来快速切换响铃模式的想法。
首先,要创建一个widget。网上搜了一下,方法很普遍。只要实现onUpdate方法,使用pendingIntent发送广播,本地再接收广播执行切换响铃模式即可。代码如下:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { Intent prevInten_ring = new Intent("RING"); Intent prevInten_vibrate = new Intent("VIBRATE"); Intent prevInten_silent = new Intent("SILENT"); PendingIntent Pprevintent_ring = PendingIntent.getBroadcast(context, 0, prevInten_ring, 0); PendingIntent Pprevintent_vibrate = PendingIntent.getBroadcast(context, 0, prevInten_vibrate, 0); PendingIntent Pprevintent_silent = PendingIntent.getBroadcast(context, 0, prevInten_silent, 0); views = new RemoteViews(context.getPackageName(), R.layout.widget); AudioManager mAudioManager = (AudioManager) context .getSystemService(Context.AUDIO_SERVICE); int mode = mAudioManager.getRingerMode(); if (mode == AudioManager.RINGER_MODE_NORMAL) { views.setImageViewResource(R.id.iv_ring, R.drawable.ring_green); views.setImageViewResource(R.id.iv_silent, R.drawable.silent_grey); views.setImageViewResource(R.id.iv_vibrate, R.drawable.vibrate_grey); } if (mode == AudioManager.RINGER_MODE_SILENT) { views.setImageViewResource(R.id.iv_ring, R.drawable.ring_grey); views.setImageViewResource(R.id.iv_silent, R.drawable.silent_green); views.setImageViewResource(R.id.iv_vibrate, R.drawable.vibrate_grey); } if (mode == AudioManager.RINGER_MODE_VIBRATE) { views.setImageViewResource(R.id.iv_ring, R.drawable.ring_grey); views.setImageViewResource(R.id.iv_silent, R.drawable.silent_grey); views.setImageViewResource(R.id.iv_vibrate, R.drawable.vibrate_green); } views.setOnClickPendingIntent(R.id.iv_ring, Pprevintent_ring); views.setOnClickPendingIntent(R.id.iv_silent, Pprevintent_silent); views.setOnClickPendingIntent(R.id.iv_vibrate, Pprevintent_vibrate); appWidgetManager.updateAppWidget(appWidgetIds, views); }上段代码中,在设置点击事件时需要注意一定要添加
appWidgetManager.updateAppWidget(appWidgetIds, views);方法,否则点击事件不会生效。
其次,需要实现onRevieve()方法,代码如下:
public void onReceive(Context context, Intent intent) { super.onReceive(context, intent); AudioManager mAudioManager = (AudioManager) context .getSystemService(Context.AUDIO_SERVICE); if (intent.getAction().equals("RING")) { mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL); refreshUI(context, R.id.iv_ring, R.drawable.ring_green); refreshUI(context, R.id.iv_silent, R.drawable.silent_grey); refreshUI(context, R.id.iv_vibrate, R.drawable.vibrate_grey); Toast.makeText(context, context.getResources().getText(R.string.ring), Toast.LENGTH_SHORT).show(); } if (intent.getAction().equals("VIBRATE")) { mAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE); refreshUI(context, R.id.iv_ring, R.drawable.ring_grey); refreshUI(context, R.id.iv_silent, R.drawable.silent_grey); refreshUI(context, R.id.iv_vibrate, R.drawable.vibrate_green); Toast.makeText(context, context.getResources().getText(R.string.vibrate), Toast.LENGTH_SHORT).show(); Vibrator vb = (Vibrator) context .getSystemService(Service.VIBRATOR_SERVICE); vb.vibrate(300); } if (intent.getAction().equals("SILENT")) { mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT); refreshUI(context, R.id.iv_ring, R.drawable.ring_grey); refreshUI(context, R.id.iv_silent, R.drawable.silent_green); refreshUI(context, R.id.iv_vibrate, R.drawable.vibrate_grey); Toast.makeText(context, context.getResources().getText(R.string.silent), Toast.LENGTH_SHORT).show(); } }
这样就大工告成了。
具体代码见:git://code.csdn.net/u013772433/soundctrl.git
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。