編輯:關於Android編程
如下代碼是監聽電話的狀態,代碼簡單不做介紹直接看代碼:
復制代碼 代碼如下:
private boolean mResumeAfterCall = false;
private PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (state == TelephonyManager.CALL_STATE_RINGING) {
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int ringvolume = audioManager
.getStreamVolume(AudioManager.STREAM_RING);
if (ringvolume > 0) {
mResumeAfterCall = (player.isPlaying() || mResumeAfterCall);
try {
stub.pause();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
// pause the music while a conversation is in progress
mResumeAfterCall = (player.isPlaying() || mResumeAfterCall);
try {
stub.pause();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (state == TelephonyManager.CALL_STATE_IDLE) {
// start playing again
if (mResumeAfterCall) {
// resume playback only if music was playing
// when the call was answered
try {
stub.resume();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mResumeAfterCall = false;
}
}
}
};
使用的時候呢,就在service 的oncreate 裡面添加,如下:
復制代碼 代碼如下:
public void onCreate() {
player = new MediaPlayer();
super.onCreate();
TelephonyManager tmgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
tmgr.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
IntentFilter filter = new IntentFilter();
filter.addAction(SERVICECMD);
filter.addAction(PAUSE_ACTION);
receiver = new MusicServiceReceiver();
registerReceiver(receiver, filter);
}
在ondestroy 裡面刪除就可以了
復制代碼 代碼如下:
public void onDestroy() {
// Log.e(TAG, "onDestroy" +player.isPlaying() );
// System.out.println("onDestroy");
unregisterReceiver(receiver);
player.release();
player = null;
TelephonyManager tmgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
tmgr.listen(mPhoneStateListener, 0);
super.onDestroy();
}
之前實現過了天氣預報的功能 但是真的好丑 真的只是實現功能 所以上一篇博客也沒有貼出圖片 這次 相對於第一個 首先是界面做了調整 其次就是 之前那個只能查看實時天氣 這個
Android 2.0之後可以通過overridePendingTransition()給Activity增加切換動畫.而在Android5.X中,Google對動畫效果
本文實例講述了Android實現偵聽電池狀態顯示、電量及充電動態顯示的方法,是Android應用程序開發中非常常用的重要功能。分享給大家供大家參考之用。具體方法如下:使用
先給最終效果圖:當我們在最下邊的gallery中切換圖片時,上面的大圖片會自動切換,切換時有動畫效果哦,很簡單的一個程序,有待完善更多的功能!activity代碼:pac