【问题标题】:how to call an activity when getting incoming call.接到来电时如何调用活动。
【发布时间】:2012-04-21 14:48:30
【问题描述】:

大家好,我想通过默认的来电活动调用我自己的活动。我已经使用广播接收器完成了此操作,我在接到来电时调用了我的活动。但是从我第二次接到来电时它第一次工作正常,然后默认的来电活动正在通过我的活动。我不知道是什么问题,谁能帮帮我..

清单:

<?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;
    }
}
}

【问题讨论】:

  • 嗨,这个问题已经解决了..感谢您对我的支持..
  • 我创建了一个线程来调用我的活动,并且我在该开始活动之前写了一条语句,即休眠 500 毫秒。那是我的解决方案...如果有人不在 statd 下然后问我我会在这里发布代码。

标签: android android-intent android-service android-input-method


【解决方案1】:

其实完整的代码是我在上面发布的,只是替换下面的代码

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 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();
    }
}
 }

【讨论】:

  • 有没有机会澄清这个话题?公开回答是不够的,公开明确的回答是路要走.. ;)
  • 上面的答案很清楚......如果你愿意,我会发布完整的代码......我的项目很忙,所以我迟到了。如果有人想要完整的源代码,请询问我将发送给您..
  • 嗨,Pandu,我也想要。当有来电时,我想显示一个活动。我尝试了你的代码,但我无法打开我的活动,你能分享完整的代码吗?
  • @pandu 嗨,感谢上述鳕鱼,但这不起作用,仍然收到来电。
  • @pandu 请您有解决方案,然后请帮助我。
【解决方案2】:

我创建了一个线程来调用我的活动,并且我在该开始活动之前写了一条语句,即睡眠 500 毫秒。那是我的解决方案...如果有人不在 statd 下,请问我,我会在这里发布代码。

【讨论】:

  • 如果对您有帮助,请找到以下答案,然后选择作为答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-08
  • 2015-04-05
  • 2019-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多