切换小部件打开时如何阻止来电?

时间:2014-10-31 11:23:09

标签: android

我有一个应用程序,其中我放置了一个开关切换按钮。我的应用程序的目的是,只要将开关按钮设置为ON,就应阻止所有来电。我可以阻止所有呼叫但是当开关设置为ON时我无法阻止。

我创建了一个名为MyClassReceiver的类,它扩展了Broadcast Receiver,所有的调用阻塞都在这个类中完成。我在Manifest文件中添加了接收器代码,该代码完美无缺。

这是我正在使用的代码: -

MainActivity.java

package com.chinmay.smsender;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;

public class MainActivity extends Activity {

public Switch mySwitch;
//private LinearLayout bgElement;
private AudioManager mAudio;
boolean switchChecked;

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

    mySwitch = (Switch) findViewById(R.id.switch1);
    //bgElement = (LinearLayout) findViewById(R.id.container);
    mAudio = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);

    mySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener(){

        @Override
        public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
            if(isChecked) {
                mAudio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                broadcastIntent(true);
                //bgElement.setBackgroundColor(Color.GREEN);
            } else {
                mAudio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                broadcastIntent(false);
                //bgElement.setBackgroundColor(Color.RED);
            } 
        }

    });
}

public void broadcastIntent(boolean isChecked) {
    Intent intent = new Intent();
    intent.setAction("com.chinmay.CUSTOM_INTENT");
    intent.putExtra("switchChecked", isChecked);
    sendBroadcast(intent);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

MyClassReceiver.java

package com.chinmay.smsender;

import java.lang.reflect.Method;

import com.android.internal.telephony.ITelephony;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.widget.Toast;


public class MyClassReceiver extends BroadcastReceiver {
private ITelephony telephonyService;
Context context = null;
boolean isChecked;

@Override
public void onReceive(Context context, Intent intent) {

    TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    isChecked = intent.getExtras().getBoolean("switchChecked");

    if(isChecked) {
        Toast.makeText(context, "Blocking calls", Toast.LENGTH_LONG).show();

        if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
            //String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
            //Toast.makeText(context, "Call from: "+incomingNumber, Toast.LENGTH_LONG).show();

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

        }
    } else if(isChecked == false) {
        Toast.makeText(context, "Blocking calls off", Toast.LENGTH_LONG).show();
    }

    /* else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE) || intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
        Toast.makeText(context, "Detected Call Hangup Event.", Toast.LENGTH_LONG).show();
    }*/
}

}

Android Manifest

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

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="19" />

<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" >
    <activity
        android:name="com.chinmay.smsender.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name="com.chinmay.smsender.MyClassReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
            <action android:name="com.chinmay.CUSTOM_INTENT" />
        </intent-filter>
    </receiver>    
</application>

</manifest>

如果我运行此代码,这是我得到的错误

10-31 13:03:26.283: E/AndroidRuntime(759): FATAL EXCEPTION: main
10-31 13:03:26.283: E/AndroidRuntime(759): Process: com.chinmay.smsender, PID: 759
10-31 13:03:26.283: E/AndroidRuntime(759): java.lang.RuntimeException: Unable to start receiver com.chinmay.smsender.MyClassReceiver: java.lang.NullPointerException
10-31 13:03:26.283: E/AndroidRuntime(759):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2441)
10-31 13:03:26.283: E/AndroidRuntime(759):  at android.app.ActivityThread.access$1700(ActivityThread.java:139)
10-31 13:03:26.283: E/AndroidRuntime(759):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286)
10-31 13:03:26.283: E/AndroidRuntime(759):  at android.os.Handler.dispatchMessage(Handler.java:102)
10-31 13:03:26.283: E/AndroidRuntime(759):  at android.os.Looper.loop(Looper.java:136)
10-31 13:03:26.283: E/AndroidRuntime(759):  at android.app.ActivityThread.main(ActivityThread.java:5086)
10-31 13:03:26.283: E/AndroidRuntime(759):  at java.lang.reflect.Method.invokeNative(Native Method)
10-31 13:03:26.283: E/AndroidRuntime(759):  at java.lang.reflect.Method.invoke(Method.java:515)
10-31 13:03:26.283: E/AndroidRuntime(759):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-31 13:03:26.283: E/AndroidRuntime(759):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-31 13:03:26.283: E/AndroidRuntime(759):  at dalvik.system.NativeStart.main(Native Method)
10-31 13:03:26.283: E/AndroidRuntime(759): Caused by: java.lang.NullPointerException
10-31 13:03:26.283: E/AndroidRuntime(759):  at com.chinmay.smsender.MyClassReceiver.onReceive(MyClassReceiver.java:49)
10-31 13:03:26.283: E/AndroidRuntime(759):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2434)
10-31 13:03:26.283: E/AndroidRuntime(759):  ... 10 more

我认为因为我向MyClassReceiver发送了两个意图,我收到了这个错误。

请告诉我如何知道MyClassReceiver本身的开关按钮状态,并在开关打开时阻止每次通话。

1 个答案:

答案 0 :(得分:0)

可以在共享首选项的帮助下访问切换小部件的布尔状态。请参阅答案:

How to get switch value in Android?

获得布尔状态后,我们只需检查它是否为真,如果是,则阻塞调用。