如何在android中以编程方式拒绝来电?

发布于 2024-09-24 20:32:40 字数 209 浏览 9 评论 0原文

可能的重复:
如何在 android 中阻止呼叫

谁能告诉我如何拒绝来电在android中以编程方式?

Possible Duplicate:
How to block calls in android

Can any one tell me how to reject incoming call programatically in android ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扛起拖把扫天下 2024-10-01 20:32:41

让我们分 3 步实施此解决方案:

a.定义此类或接收器:

这是您的主类:

package com.sample;
import java.lang.reflect.Method;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

import com.android.internal.telephony.*;


public class main extends BroadcastReceiver {
private static final String TAG = null;
String incommingNumber;
String incno1= "9916090941";

public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();

    if(null == bundle)
            return;
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);       
    try {
        // Java reflection to gain access to TelephonyManager's
        // ITelephony getter
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        Log.v(TAG, "Get getTeleService...");
        Class c = Class.forName(tm.getClass().getName());
        Method m = c.getDeclaredMethod("getITelephony");
        m.setAccessible(true);
        com.android.internal.telephony.ITelephony telephonyService = (ITelephony) m.invoke(tm);  
        Bundle b = intent.getExtras();
        incommingNumber = b.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
        Log.v(TAG,incommingNumber );
        Log.v(TAG,incno1 );
        if ( incommingNumber.equals(incno1) )
        {
             telephonyService = (ITelephony) m.invoke(tm);
               telephonyService.silenceRinger();
        telephonyService.endCall();
        Log.v(TAG,"BYE BYE BYE" );
        }
        else{

        telephonyService.answerRingingCall();
        Log.v(TAG,"HELLO HELLO HELLO" );
        }


    } catch (Exception e) {
        e.printStackTrace();
        Log.e(TAG,
                "FATAL ERROR: could not connect to telephony subsystem");
        Log.e(TAG, "Exception object: " + e);
    }


    }

}

b.清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.sample"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
       <receiver  android:name=".main">
            <intent-filter  android:priority="100" >
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>

    </application>
    <uses-sdk android:minSdkVersion="7" />
 <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" />
</manifest> 

c.在com.android.internal.telephony mh下定义电话aidl

 package com.android.internal.telephony;

  interface ITelephony { 
    boolean endCall();

    void answerRingingCall();

    void silenceRinger();
 }

:仅在模拟器中为我工作,而不是在真实设备上...android 2.3以上的所有设备都需要root权限或安装为系统应用程序才能使用该权限<代码>android.permission.MODIFY_PHONE_STATE。

Let us implement this solution in 3 steps :

a. Define this class or receiver :

This is your main class :

package com.sample;
import java.lang.reflect.Method;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

import com.android.internal.telephony.*;


public class main extends BroadcastReceiver {
private static final String TAG = null;
String incommingNumber;
String incno1= "9916090941";

public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();

    if(null == bundle)
            return;
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);       
    try {
        // Java reflection to gain access to TelephonyManager's
        // ITelephony getter
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        Log.v(TAG, "Get getTeleService...");
        Class c = Class.forName(tm.getClass().getName());
        Method m = c.getDeclaredMethod("getITelephony");
        m.setAccessible(true);
        com.android.internal.telephony.ITelephony telephonyService = (ITelephony) m.invoke(tm);  
        Bundle b = intent.getExtras();
        incommingNumber = b.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
        Log.v(TAG,incommingNumber );
        Log.v(TAG,incno1 );
        if ( incommingNumber.equals(incno1) )
        {
             telephonyService = (ITelephony) m.invoke(tm);
               telephonyService.silenceRinger();
        telephonyService.endCall();
        Log.v(TAG,"BYE BYE BYE" );
        }
        else{

        telephonyService.answerRingingCall();
        Log.v(TAG,"HELLO HELLO HELLO" );
        }


    } catch (Exception e) {
        e.printStackTrace();
        Log.e(TAG,
                "FATAL ERROR: could not connect to telephony subsystem");
        Log.e(TAG, "Exception object: " + e);
    }


    }

}

b. Manifest file :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.sample"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
       <receiver  android:name=".main">
            <intent-filter  android:priority="100" >
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>

    </application>
    <uses-sdk android:minSdkVersion="7" />
 <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" />
</manifest> 

c . Define telephone aidl , under com.android.internal.telephony

 package com.android.internal.telephony;

  interface ITelephony { 
    boolean endCall();

    void answerRingingCall();

    void silenceRinger();
 }

mh: Worked for me only in emulator, not on real devices... All devices above android 2.3 require root permission or installation as a system app to be able to use the permission android.permission.MODIFY_PHONE_STATE.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文