使用广播接收器发送多个文本的Android自动文本回复。

时间:2016-04-22 22:50:45

标签: java android android-studio broadcastreceiver smsmanager

以下代码将文本发送到传入的主叫号码,但每次都会发送不同数量的文本。有时它只发送一个,但它最多变化为6.当我的代码在其运行时监控我也得到不同数量的广播。我做错了什么?

接收器:

package com.biapps.makin_biscuits;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by Jake on 3/21/2016.
 */

public class IncomingCallReceiver extends BroadcastReceiver {

private static final String TAG = "MyListener";
    private Context mContext;
    private Intent mIntent;

    @Override
    public void onReceive(Context context, Intent intent) {
        mContext = context;
        mIntent = intent;
        TelephonyManager tm = (TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE);
        int events = PhoneStateListener.LISTEN_CALL_STATE;

        tm.listen(phoneStateListener, events);
    }

    private final PhoneStateListener phoneStateListener = new PhoneStateListener() {
        @Override

        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);

            Log.i(TAG, "number: " + incomingNumber + "");
            Toast.makeText(mContext,"Incoming Call: " + incomingNumber + "",Toast.LENGTH_LONG).show();
            switch (state) {
                case TelephonyManager.CALL_STATE_RINGING:

                        SmsManager smsManager =     SmsManager.getDefault();
                        smsManager.sendTextMessage(incomingNumber, null, "I'm busy bruh", null, null);
                    TelephonyManager tm = (TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE);
                    tm.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);

            }

                   }
    };
}

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.biapps.makin_biscuits">
    <uses-sdk android:minSdkVersion="4" />

    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.SEND_SMS"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

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

        <receiver
            android:name=".IncomingCallReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
                <action android:name="android.intent.action.Trigger" />

            </intent-filter>
        </receiver>
    </application>

</manifest>

MainActivity:

package com.biapps.makin_biscuits;

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    //set object labels and states here
    private ImageButton icon;
    private AudioManager am;
    private ImageButton people;
    private ImageButton ring;

    @Override

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


        am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

        icon = (ImageButton) findViewById(R.id.icon);
        icon.setOnClickListener(imgButtonHandler);
        people = (ImageButton) findViewById(R.id.people);
        //people.setOnClickListener(peopleButtonHandler);
        ring = (ImageButton) findViewById(R.id.ring);

    }

    int buttonstate = 0;
    public View.OnClickListener imgButtonHandler = new View.OnClickListener() {

        public void onClick(View v) {
            if (buttonstate == 0) {

                icon.setImageResource(R.drawable.buttonup);
                buttonstate = 1;
                am.setRingerMode(0);
                Toast.makeText(getApplicationContext(), "Go!!",
                        Toast.LENGTH_SHORT).show();

                Intent intent = new Intent();
                intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
                intent.setAction("android.intent.action.Trigger");
                sendBroadcast(intent);
            } else {

                icon.setImageResource(R.drawable.button);
                buttonstate = 0;
                am.setRingerMode(2);
                Toast.makeText(getApplicationContext(), "Come back!!",
                        Toast.LENGTH_SHORT).show();
            }
        }
    };
}

2 个答案:

答案 0 :(得分:1)

我添加了一些代码来识别以前的状态并确定是否执行操作。这解决了这个问题。

package com.biapps.makin_biscuits;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by Jake on 3/21/2016.
 */

public class IncomingCallReceiver extends BroadcastReceiver {

private static final String TAG = "MyListener";
    private Context mContext;
    private Intent mIntent;

    @Override
    public void onReceive(Context context, Intent intent) {
        mContext = context;
        mIntent = intent;
        TelephonyManager tm = (TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE);
        int events = PhoneStateListener.LISTEN_CALL_STATE;

        tm.listen(phoneStateListener, events);
    }

    int previousState=TelephonyManager.CALL_STATE_OFFHOOK;
    private final PhoneStateListener phoneStateListener = new PhoneStateListener() {
        @Override

        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);

            int Call_State_Ringing = TelephonyManager.CALL_STATE_RINGING;
            int Call_State_OffHook = TelephonyManager.CALL_STATE_OFFHOOK;
            if (Call_State_Ringing == state && Call_State_OffHook == previousState){
            Log.i(TAG, "number: " + incomingNumber + "");
            Toast.makeText(mContext,"Incoming Call: " + incomingNumber + "",Toast.LENGTH_LONG).show();
            switch (state) {
                case TelephonyManager.CALL_STATE_RINGING:

                        SmsManager smsManager =     SmsManager.getDefault();
                        smsManager.sendTextMessage(incomingNumber, null, "I'm busy bruh", null, null);
                    TelephonyManager tm = (TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE);
                    tm.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);

            }

                   }
            previousState = state;
    }
};}

答案 1 :(得分:0)

在您的代码中:

您正在onReceive()中注册PhoneStateListener,因此无论何时调用onReceive方法,都会向PhoneStateListener添加一个TelephonyManager。因此,在此期间,您可以为同一事件提供多个侦听器。

你可以做的是,你需要检查之前是否添加了监听器,如果是这样的话就跳过那部分。