我的应用程序适用于一些但不适用于其他人,我无法弄清楚原因

时间:2016-04-14 09:13:37

标签: android broadcastreceiver sms android-service call

我的App Drive Drive可以找到here。 Drive Safe是一款免费应用程序,可防止分心驾驶。当应用程序启动时,它会自动回复给您打电话或发短信的任何人。

我有2个测试手机,它在它们上运行得很完美,并且在朋友的手机上进行了测试,在这个阶段最多可以测试20个,而且它也很适合它们,但是我收到了来自世界各地的人的电子邮件告诉我,Drive Safe并不适合他们。要么根本不工作,只能将电话置于静音状态,要么对其他回复电话而不是文本,对其他人则不然。 我已经改变了一些我认为可能是问题区域的代码,但我仍然收到来自新人的电子邮件说他们遇到了问题。

我不确定该应用有多少人使用的百分比,即10%,%50,%80或%90。我可以看到我在上周有近1000个会话和762个用户返回用户。

我的应用主要在后台运行,并使用服务。也是一个收听短信和电话的广播接收器。

这是我的一些代码: 按钮的代码,用于关闭应用程序或关闭。

// listen for clicks on the main button
    buttonToggleDetect.setOnClickListener(new View.OnClickListener() {
        @Override
        // when the main button is clicked
        public void onClick(View v) {
            setDetectEnabled(!detectEnabled);
            mTracker.send(new HitBuilders.EventBuilder()
                    .setCategory("Home")
                    .setAction("Share")
                    .build());


            if (detectEnabled) {
                showStatusBarIcon(true);
            } else
                showStatusBarIcon(false);
        }
    });

用于在按下主按钮时关闭服务的代码

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void setDetectEnabled(boolean enable) {

    detectEnabled = enable;
    Intent intent = new Intent(this, CallDetectService.class); //creating a new intent to be sent to CallDetectService class


    SharedPreferences sharedPreferences = getSharedPreferences("driveSafeMode", Context.MODE_PRIVATE);

    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean("mode", enable);
    editor.apply();


    if (enable) {
        startService(intent);
        setRingerSilent();

    } else {
        // stop detect service
        stopService(intent);
        setRingerNormal();
    }
    toggleUI();
}

Call Helper类,用于处理收到的呼叫并将文本发送回传入的号码。

private String phoneNumber ="0";
private int count=0;
private Boolean messageSent = false;

private final BroadcastReceiver mybroadcast = new IncomingSms();


public CallHelper() {}

// Listener to detect incoming calls.
public class  CallStateListener extends PhoneStateListener{
    @Override


    public  void onCallStateChanged (int state, String incomingNumber) {
        switch (state){

            case TelephonyManager.CALL_STATE_RINGING:
                // case when someone is calling the users phone

                //Creating a object of the AudioManager Class
                AudioManager audioManager = (AudioManager) ctx.getSystemService(Activity.AUDIO_SERVICE);


                // Emergency mode code is below till 2nd else statement
                // Emergency mode is when a person calls 3 times in a row, on the third time
                // the app will be switched off and the phone will ring aloud.
                // if the saved variable phoneNumber is == to the current incoming number trigger if
                if (phoneNumber.equalsIgnoreCase(incomingNumber)) {
                    count++;


                    if (count >=2){
                        // stop app
                        ctx.stopService(new Intent(ctx, CallDetectService.class));
                        stop();

                        Toast.makeText(ctx, R.string.emergencyModeString, Toast.LENGTH_LONG).show();
                        phoneNumber = "0";
                        audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                        count = 0;

                    } else {
                        // this is to prevent the Drive Safe Message being sent muliple times in receipt of one text/call
                        // Drive safe Message should only be sent once per message or call
                        if (!messageSent) {

                            SendMessage();
                            messageSent = true;
                        }
                    }

                } else {
                    phoneNumber = incomingNumber.toString();
                    if (!messageSent) {
                        SendMessage();
                        messageSent = true;
                        // if the phone is not == to previous incoming call and
                        // the Drive Safe Message has not already been sent, send it.
                    }
                }
                break;

            // whent the phone is not ringing and in a idle state set messageSent = to false
            case TelephonyManager.CALL_STATE_IDLE:
                messageSent = false;
        }
    }
}


private Context ctx;
private TelephonyManager tm;
private CallStateListener callStateListener;


// Constructor
public CallHelper(Context ctx){
    this.ctx = ctx;
    tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
    callStateListener = new CallStateListener();
}

//Method
public void SendMessage(){

    String sms = MainActivity.driveSafeSms;

    try {
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(phoneNumber, null, sms, null, null);

        Toast.makeText(ctx, R.string.receivedCall, Toast.LENGTH_LONG).show();

    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(ctx, R.string.smsFailed, Toast.LENGTH_LONG).show();
    }
}

// Start Call Detection.
public void start() {
    tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}


// Stop Call Dectection
public void stop() {
    tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
}

} `

CallDetectService这是服务类

// create instance of CallHelper class
private CallHelper callHelper;

private final BroadcastReceiver mybroadcast = new IncomingSms();
IntentFilter filter = new IntentFilter();
private Boolean listeningSms = false;


public CallDetectService() {}

@Override
public int onStartCommand(Intent intent, int flags, int startId){


    //Register Broadcast Reciever
    //if the sms BroadcastReceiver has not already been registered register it
    if (!listeningSms) {
        filter.addAction("android.provider.Telephony.SMS_RECEIVED");
        registerReceiver(mybroadcast, filter);
        listeningSms = true;
    }




    callHelper = new CallHelper(this);

    int res = super.onStartCommand(intent, flags, startId);
    callHelper.start();

    return res;
    }

@Override
public void onDestroy(){
    super.onDestroy();
    callHelper.stop();


    // if the sms broadcast reciever has been registered, unregister it
    if (listeningSms == true) {
        unregisterReceiver(mybroadcast);
        listeningSms = false;
    }


    Toast.makeText(getApplicationContext(), R.string.inActive, Toast.LENGTH_SHORT).show();
}

@Override
public IBinder onBind (Intent intent) {
        return  null;
}

}
我很抱歉要输入这么多代码。但我真的无法想象我哪里出错了,尤其是我不能自己复制这些错误,以便修复它们。我所寻找的只是一个指针和一些建议。

0 个答案:

没有答案