2016-05-30 75 views
0

我做了一些谷歌搜索,但無法找到明確的答案,我的問題。我怎樣才能取代我自己的「電話鈴聲」屏幕,而不是android默認的?

我有一個活動,需要啓動時,手機開始響鈴(接到電話) - 而不是androids默認屏幕。

enter image description here

我知道我必須設置的Manifest.xml接收器:

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

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

,並創建一個接收器類:

public class MyPhoneStateListener extends PhoneStateListener { 
 

 
    public static Boolean phoneRinging = false; 
 

 
    public void onCallStateChanged(int state, String incomingNumber) { 
 

 
     switch (state) { 
 
      case TelephonyManager.CALL_STATE_IDLE: 
 
       Log.e("DEBUG", "IDLE"); 
 
       phoneRinging = false; 
 
       break; 
 
      case TelephonyManager.CALL_STATE_OFFHOOK: 
 
       Log.e("DEBUG", "OFFHOOK"); 
 
       phoneRinging = false; 
 
       break; 
 
      case TelephonyManager.CALL_STATE_RINGING: 
 
       //Intent intent=new Intent(getClass().) 
 
       Log.e("DEBUG", "RINGING"); 
 
       phoneRinging = true; 
 

 
       break; 
 
     } 
 
    } 
 

 
} 
 

 
class ServiceReceiver extends BroadcastReceiver { 
 
    TelephonyManager telephony; 
 
    Intent in; 
 
    public void onReceive(Context context, Intent intent) { 
 
     MyPhoneStateListener phoneListener = new MyPhoneStateListener(); 
 
     telephony = (TelephonyManager) context 
 
       .getSystemService(Context.TELEPHONY_SERVICE); 
 
     telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE); 
 
    
 
    } 
 
}

現在我可以搶我的「RINGING」 logcat正確,但我怎樣才能開始我的自定義活動或什麼是這樣做的首選方式?

回答

0

我找到了答案,我打算與你分享。

首先與onCallStateChanged部分無關。 ,所以我必須調用對象的活動onReceive廣播接收器類中:

@Override 
 
    public void onReceive(Context context, Intent intent) { 
 
     Bundle extras = intent.getExtras(); 
 
     if (extras != null) { 
 
      String state = extras.getString(TelephonyManager.EXTRA_STATE); 
 
      if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { 
 
       Intent intent1=new Intent(context, Ringing.class); 
 
       intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
 

 
       context.startActivity(intent1); 
 
       
 
      } 
 
     } 
 
    }

相關問題