3

大家好,我想調用我自己的活動默認來電活動..我已經完成了這與使用廣播接收器,我正在調用我的活動時,來電。但是當我接到來電時第一次從第一次開始工作正常,然後默認來電呼叫活動超過了我的活動。我不知道是什麼問題可以在任何一個請幫助我..如何在來電時致電活動。

清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.satish.service" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk android:minSdkVersion="8" /> 

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

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 
    <receiver 
     android:name="MyCallReceiver" 
     android:enabled="true" > 
     <intent-filter android:priority="10"> 
      <action android:name="android.intent.action.PHONE_STATE" > 

      </action> 
     </intent-filter> 
    </receiver> 
    <receiver 
     android:name="TestReceiver" 
     android:enabled="true" > 
     <intent-filter android:priority="10"> 
      <action android:name="jason.wei.custom.intent.action.TEST" > 
      </action> 
     </intent-filter> 
    </receiver> 

    <activity 
     android:name="CallActivity" 
     android:clearTaskOnLaunch="true" 
     android:launchMode="singleTask" > 
     <intent-filter android:priority="1000"></intent-filter> 
    </activity> 
</application> 
</manifest> 

Java代碼:

public class MyCallReceiver extends BroadcastReceiver{ 
public static final String CUSTOM_INTENT = "jason.wei.custom.intent.action.TEST"; 
Context context = null; 
private static final String TAG = "Phone call"; 



public void onReceive(Context context, Intent intent) { 

    Bundle extras = intent.getExtras(); 
    if (extras != null) { 
     String state = extras.getString(TelephonyManager.EXTRA_STATE); 
     Log.w("DEBUG", state); 
     if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { 

        Intent i = new Intent(); 
        i.setAction(CUSTOM_INTENT); 
        context.sendBroadcast(i); 

     } 
    } 
} 
} 


public class TestReceiver extends BroadcastReceiver { 

public void onReceive(Context context, Intent intent) { 
    if (intent.getAction().equals(MyCallReceiver.CUSTOM_INTENT)) { 
     System.out.println("GOT THE INTENT"); 
     context.startActivity(new Intent(context, CallActivity.class) 
       .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); 
    } 
} 
} 

public class CallActivity extends Activity implements OnClickListener{ 
private ITelephony telephonyService; 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    setTheme(android.R.style.Theme_Dialog); 
    ((Button)findViewById(R.id.call)).setOnClickListener(this); 
    ((Button)findViewById(R.id.end)).setOnClickListener(this); 
    TelephonyManager telephony = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); 

     try { 
      Class c = Class.forName(telephony.getClass().getName()); 
      Method m = c.getDeclaredMethod("getITelephony"); 
      m.setAccessible(true); 
      telephonyService = (ITelephony) m.invoke(telephony); 
      } catch (Exception e) { 
      e.printStackTrace(); 
      } 
} 

@Override 
public void onClick(View arg0) { 
    switch (arg0.getId()) { 
    case R.id.call: 

     telephonyService.answerRingingCall(); 
     break; 

    case R.id.end: 
     telephonyService.endCall(); 
     finish(); 
     break; 

    default: 
     break; 
    } 
} 
} 
+0

嗨得到sloved這個問題替換下面的代碼

public class TestReceiver extends BroadcastReceiver{ public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(MyCallReceiver.CUSTOM_INTENT)) { System.out.println("GOT THE INTENT"); context.startActivity(new Intent(context, CallActivity.class) .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); } } } 

..感謝對我的支持.. – pandu 2012-04-09 07:22:52

+0

我創建了一個線程來調用我的活動,並且在那個睡眠時間爲500ms的開始活動之前我寫了一條語句。這是我的解決方案...如果有人不在statd然後問我,我會在這裏發佈代碼。 – pandu 2012-04-10 07:00:36

回答

3

其實全代碼爲我張貼aboove只是下面的代碼

public class TestReceiver extends BroadcastReceiver{ 

@Override 
public void onReceive(final Context context, Intent intent) { 
    if (intent.getAction().equals(MyCallReceiver.CUSTOM_TEST_INTENT)) { 
     System.out.println("GOT THE INTENT"); 
     final String mobileNumber = intent.getExtras().getString("number"); 
     Thread thread = new Thread(){ 
      private int sleepTime = 400; 

      @Override 
      public void run() { 
       super.run(); 
       try { 
        int wait_Time = 0; 

        while (wait_Time < sleepTime) { 
         sleep(100); 
         wait_Time += 100; 
        } 
       }catch (Exception e) { 
        Toast.makeText(context, 
          "Error Occured Because:" + e.getMessage(), 
          Toast.LENGTH_SHORT).show(); 
       } 
       finally { 

       } 

       context.startActivity(new Intent(context, CallActivity.class).putExtra("number", mobileNumber) 
         .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); 
      } 
     }; 
     thread.run(); 
    } 
} 
} 
+0

這個話題有沒有可能被澄清? 公衆回答是不夠的,公開的答案是去的方式..;) – xliiv 2012-06-21 08:55:53

+0

上面的答案很清楚...如果你想我會發布完整的代碼...我很忙我的項目這就是爲什麼我是晚了。如果有人想要完整的源代碼,請問我會發給你.. – pandu 2012-06-25 11:19:01

+0

喜潘杜,我想要的一樣。當有來電時,我想顯示一個活動。我試過你的代碼,但是我沒有打開我的活動,你能分享完整的代碼嗎? – 2013-04-30 08:59:15

1

我創建了一個線程調用我的活動,並在我在開始500ms的睡眠活動之前寫了一條聲明。這是我的解決方案...如果有人不在statd然後問我,我會在這裏發佈代碼。

+0

我剛剛在TestReceiver類 – pandu 2012-04-10 07:02:31

+0

中做了一些修改,可以發表你的答案嗎? – KMI 2012-04-16 11:42:03

+0

你可以發佈你的完整代碼嗎? – swathi 2012-05-31 05:21:16