我正在尝试创建一个可以阻止来电的应用程序。我得到ClassNotFoundException
了。TelephonyManager
以下是我正在使用的代码。我在这里做错了什么?
广播接收器
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.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
public class TelephoneBroadcastReceiver extends BroadcastReceiver {
String TAG = "My Tag";
String incomingNumber = "";
String incno1 = "5556";
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.d(TAG, "INF: Broadcast received.");
try{
TelephonyManager tManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Log.v(TAG, "Get getTeleService...");
Class c = Class.forName(tManager.getClass().getSimpleName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
com.android.internal.telephony.ITelephony telephonyService = (ITelephony) m.invoke(tManager);
Bundle bundle = intent.getExtras();
incomingNumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.v(TAG,incomingNumber );
Log.v(TAG,incno1 );
if ( incomingNumber.equals(incno1) ) {
telephonyService = (ITelephony) m.invoke(tManager);
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);
}
}
}
清单文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.atiffarrukh.callandsmsfilter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="com.atiffarrukh.callandsmsfilter.broadcastreceiver.TelephoneBroadcastReceiver" >
<intent-filter >
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
现在错误
08-02 13:21:20.641: D/My Tag(557): INF: Broadcast received.
08-02 13:21:20.641: V/My Tag(557): Get getTeleService...
08-02 13:21:20.641: W/System.err(557): java.lang.ClassNotFoundException: TelephonyManager
08-02 13:21:20.641: W/System.err(557): at java.lang.Class.classForName(Native Method)
08-02 13:21:20.641: W/System.err(557): at java.lang.Class.forName(Class.java:235)
08-02 13:21:20.641: W/System.err(557): at java.lang.Class.forName(Class.java:182)
08-02 13:21:20.641: W/System.err(557): at com.atiffarrukh.callandsmsfilter.broadcastreceiver.TelephoneBroadcastReceiver.onReceive(TelephoneBroadcastReceiver.java:30)
08-02 13:21:20.641: W/System.err(557): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2810)
08-02 13:21:20.641: W/System.err(557): at android.app.ActivityThread.access$3200(ActivityThread.java:125)
08-02 13:21:20.641: W/System.err(557): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
08-02 13:21:20.641: W/System.err(557): at android.os.Handler.dispatchMessage(Handler.java:99)
08-02 13:21:20.641: W/System.err(557): at android.os.Looper.loop(Looper.java:123)
08-02 13:21:20.764: W/System.err(557): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-02 13:21:20.764: W/System.err(557): at java.lang.reflect.Method.invokeNative(Native Method)
08-02 13:21:20.764: W/System.err(557): at java.lang.reflect.Method.invoke(Method.java:521)
08-02 13:21:20.764: W/System.err(557): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-02 13:21:20.764: W/System.err(557): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-02 13:21:20.764: W/System.err(557): at dalvik.system.NativeStart.main(Native Method)
08-02 13:21:20.764: W/System.err(557): Caused by: java.lang.NoClassDefFoundError: TelephonyManager
08-02 13:21:20.764: W/System.err(557): ... 15 more
08-02 13:21:20.764: W/System.err(557): Caused by: java.lang.ClassNotFoundException: TelephonyManager in loader dalvik.system.PathClassLoader[/data/app/com.atiffarrukh.callandsmsfilter-1.apk]
08-02 13:21:20.764: W/System.err(557): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
08-02 13:21:20.764: W/System.err(557): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
08-02 13:21:20.764: W/System.err(557): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
08-02 13:21:20.764: W/System.err(557): ... 15 more
08-02 13:21:20.764: E/My Tag(557): FATAL ERROR: could not connect to telephony subsystem
08-02 13:21:20.764: E/My Tag(557): Exception object: java.lang.ClassNotFoundException: TelephonyManager
谢谢...