我需要帮助,我发现了一个相同的问题,但对我没有用。
这是我的代码:
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.provider.CallLog;
import android.provider.ContactsContract;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.ArrayAdapter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class CallDetector extends Service {
static String[] test = new String[15];
boolean isstart = false;
AudioManager audio_mng;
private static final String DEBUG_TAG = "InviteActivity";
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
CallDetector calldetector= new CallDetector(this);
Log.v(DEBUG_TAG, "Service start");
int i = 0;
File file = new File("pathtofile/myfile.txt");
try {
Scanner scaner = new Scanner(new FileReader(file));
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
do{
line = scaner.nextLine();
test[i] = line;
i++;
br.close();
}while(scaner.hasNext());
}
catch (IOException e) {
//You'll need to add proper error handling here
}
calldetector.start();
return START_STICKY;
}
public void onDestroy(){
CallDetector calldetector = new CallDetector(this);
calldetector.stop();
super.onDestroy();
}
public void onCreate(){
super.onCreate();
audio_mng = (AudioManager) getBaseContext().getSystemService(ctx.AUDIO_SERVICE);
}
private class PhoneCallListener extends PhoneStateListener {
private boolean isPhoneCalling = false;
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (TelephonyManager.CALL_STATE_RINGING == state) {
for(int i =0; i <15; i++) {
if (incomingNumber.equals(test[i])) {
Log.v(DEBUG_TAG, "OK");
onchangetonormal();
}
}
}
}
}
private Context ctx;
private TelephonyManager tm;
private PhoneCallListener callStateListener;
public void onchangetonormal(){
audio_mng.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
public CallDetector(Context ctx) {
this.ctx = ctx;
callStateListener = new PhoneCallListener();
}
/**
* Start calls detection.
*/
public void start() {
Log.v(DEBUG_TAG, "Start listening");
tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
/**
* Stop calls detection.
*/
public void stop() {
tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
}
}
当我接到服务运行的电话时,我收到错误,但服务仍然在运行。
我的第一个代码是:
if (TelephonyManager.CALL_STATE_RINGING == state) {
for(int i =0; i <15; i++) {
if (incomingNumber.equals(test[i])) {
Log.v(DEBUG_TAG, "OK");
AudioManager audio_mng;
audio_mng = (AudioManager) getBaseContext().getSystemService(ctx.AUDIO_SERVICE);
audio_mng.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
}
}
当手机接到电话时,所有人都在if! 我找到了一个解决方案,正如我所说的那样,我尝试将它们放入onCreat中,然后在没有任何内容的情况下调用它。
编辑:
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.test.testapp.CallDetector.onchangetonormal(CallDetector.java:156)
at com.test.testapp.CallDetector$PhoneCallListener.onCallStateChanged(CallDetector.java:107)
at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:393)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4867)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)
谢谢!
抱歉我的英语不好!
答案 0 :(得分:0)
在NullPointerException
方法中,onchangetonormal
方法中出现异常,表示audio_mng
为null
。
在audio_mng
方法中使用onchangetonormal
对象之前进行空检查:
if(audio_mng==null){
audio_mng=(AudioManager)CallDetector.this.getSystemService(
Context.AUDIO_SERVICE);
}
答案 1 :(得分:0)
我今天有个主意.... 将AudioManager audio_mng声明为静态.... 那很有效!
如果您有其他解释或其他答案,或者为什么会这样,请回答。
感谢您的关注