2013-06-28 44 views
0

如果電池電量嚴重不足,我需要我的應用程序在來電時彈出消息。有沒有辦法做到這一點(如果可能的話切斷電話)?在網絡搜索沒有幫助我很多。在來電時彈出消息/

+0

第一 - 抓不一致請致電:http://www.tutorialforandroid.com/2009/01/get-phone-state-when-someone-is-calling_22.html第二 - 閱讀電池狀態:http://www.tutorialforandroid.com/2009/ 01/getting-battery-information-on-android.html第三方來電:http://stackoverflow.com/questions/15481524/how-to-programatically-answer-end-a-call-in-android-4 -1對於所有這些,你必須使用'BradcastReceivers'和'Intents'。而且,我想,你需要有一個「服務」才能完成你的任務 –

+0

你究竟在網上找不到什麼......? –

+0

手機響起時,我可以烤麪包或彈出對話框嗎? –

回答

0

使用該用於顯示來電

public class IncomingCallReciever extends BroadcastReceiver { 

private Context mContext; 
private Intent mIntent; 

@Override 
public void onReceive(Context context, Intent intent) { 
    mContext = context; 
    mIntent = intent; 
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 
    int events = PhoneStateListener.LISTEN_CALL_STATE; 
    tm.listen(phoneStateListener, events); 
} 

private final PhoneStateListener phoneStateListener = new PhoneStateListener() { 
    @Override 
    public void onCallStateChanged(int state, String incomingNumber) { 
     String callState = "UNKNOWN"; 
     switch (state) { 
     case TelephonyManager.CALL_STATE_IDLE: 
      callState = "IDLE"; 



      break; 
     case TelephonyManager.CALL_STATE_RINGING: 
      // write your code display Toast or Dialog 







      break; 

     } 
     Log.i(">>>Broadcast", "onCallStateChanged " + callState); 
     super.onCallStateChanged(state, incomingNumber); 
    } 
}; 

}期間出現

聲明這樣在你menifest文件

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

添加以下權限

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>