一些Manager类整理

坚强是说给别人听的谎言 提交于 2020-12-06 05:29:57

电话TelephonyManager:

<receiver android:name=".PhoneReceiver">
         <intent-filter>
          <action android:name="android.intent.action.PHONE_STATE"/>
  <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
         </intent-filter>
        </receiver>

<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission>
public class PhoneReceiver extends BroadcastReceiver {
	@Override
	public void onReceive(final Context context, Intent intent) {
		if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){
			// outgoing call 
		} else {
			// incoming call
			TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);  
			tm.listen(new PhoneStateListener() {
				public void onCallStateChanged(int state, String incomingNumber) {
					
					super.onCallStateChanged(state, incomingNumber);
					Intent intent = new Intent(context, MusicService.class);
					switch (state) {
					case TelephonyManager.CALL_STATE_IDLE:
						
					    break;
					   case TelephonyManager.CALL_STATE_OFFHOOK:

					    break;
					   case TelephonyManager.CALL_STATE_RINGING:
						 
					    break;
					}
				}
			}, PhoneStateListener.LISTEN_CALL_STATE);
		}
	}

}

NotificationManager:

NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification noti = new Notification(android.R.drawable.stat_notify_more, "notification", System.currentTimeMillis());		
CharSequence title = "notification title";
CharSequence details = "notification details";
Intent notificationIntent = new Intent(context, MusicService.class);
PendingIntent pending = PendingIntent.getService(context, 0, notificationIntent, 0);
noti.setLatestEventInfo(context, title, details, pending);
noti.flags |= Notification.FLAG_AUTO_CANCEL;
// show the notification
nm.notify(id, noti);
// cancel it
nm.cancel(id);

闹钟AlarmManager:

Intent alarmIntent = new Intent(context, AlarmReceiver.class);
PendingIntent pend = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 0);				
AlarmManager am = (AlarmManager) context.getSystemService(Context.Alarm_Service);
// set the alarm
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), interval, pend);
// cancel the alarm
am.cancel(pend);

AlarmManager.set()和AlarmManager.setRepeating()的API说明中有一句:“If there is already an alarm scheduled for the same IntentSender, it will first be canceled. ”,如果用以上的代码设置有多个闹钟,则只有最后一个会起作用,解决的方法之一是:代码中第二句PendingIntent.getBroadcast()中的第二个参数,使得每个闹钟的这个参数值不同

另外,闹钟应用的话要在每次启动手机时在一个broadcastReceiver中设置闹钟

音量控制AudioManager:

AudioManager audioMgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int flag = AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_PLAY_SOUND;
audioMgr.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, flag);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!