挂断并拒绝来自自定义活动[按钮] Android的来电

时间:2014-03-04 21:32:07

标签: java android broadcastreceiver

我正在通过学​​习Android的过程,我偶然发现了这个问题。

我想做一个“自定义来电屏幕”。我当前的解决方案是一个从BroadcastReceiver扩展的类(IncomingCallInterceptor)。在IncomingCallInterceptor类中,当手机振铃时,我覆盖onReceive并用布局启动我的活动(MainActivity)。

在该活动(MainActivity)中我有三个按钮: 接听电话,挂机,拒绝电话

这些按钮应该按照他们的说法进行操作,接听电话,挂断电话或拒绝接听电话。

我在某种程度上接受了“接听电话”,但没有挂机和拒绝。

下面是我的代码:

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.uppgift.six.one.incoming61.sixone" >

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <receiver android:name="IncomingCallInterceptor">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE"/>
            </intent-filter>
        </receiver>
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
        </activity>
    </application>
</manifest>

从BroadcastReceiver扩展的IncomingCallInterceptor:

public class IncomingCallInterceptor extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Context appContext = context.getApplicationContext();
        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        String msg = "Phone state changed to " + state;

        if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) {
            String incomingNumber = intent.getStringExtra
                    (TelephonyManager.EXTRA_INCOMING_NUMBER);
            msg += ". Incoming number is this " + incomingNumber;

            //START MY ACTIVITY!
            Intent i = new Intent(appContext, MainActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            appContext.startActivity(i);
        }
        Toast.makeText(context, msg, Toast.LENGTH_LONG).show();

    }

}

这是我的活动(布局没什么可发布的,刚才基本上是三个按钮)

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnAnswer = (Button)findViewById(R.id.btnAnswer);
        Button btnDecline= (Button)findViewById(R.id.btnDecline);
        Button btnHangUp= (Button)findViewById(R.id.btnHangUp);

        btnAnswer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON);
                i.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP,
                        KeyEvent.KEYCODE_HEADSETHOOK));
                sendOrderedBroadcast(i, null);
            }
        });

        btnDecline.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Decline Call (I need help here)
            }
        });

        btnHangUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Hang Up Call (I need help here)
            }
        });
    }

在MainActivity类中,它由注释标记,我需要一些帮助。 我也看到了一些关于“Telephonyservice interface”(thingy)解决方案的内容,但我不明白当我测试它时它是如何工作的。

1 个答案:

答案 0 :(得分:0)

拒绝来电:

 try {

                TelephonyManager tm = (TelephonyManager)
                        getSystemService(Context.TELEPHONY_SERVICE);
                try {
                    Class c = Class.forName(tm.getClass().getName());
                    Method m = c.getDeclaredMethod("getITelephony");
                    m.setAccessible(true);
                    Object telephonyService = m.invoke(tm); // Get the internal ITelephony object
                    c = Class.forName(telephonyService.getClass().getName()); // Get its class
                    m = c.getDeclaredMethod("endCall"); // Get the "endCall()" method
                    m.setAccessible(true); // Make it accessible
                    m.invoke(telephonyService); // invoke endCall()

                } catch (Exception e) {
                    e.printStackTrace();
                }


            } catch (Exception e) {

            }
        }