Java Android自动短信回复电话不工作
我已经创建了一个应用程序,可以启动广播接收器类,该类应用程序可以通过短信自动响应电话呼叫。接收器正在工作,但出现以下异常: java.lang.IllegalArgumentException:无效的destinationAddress 请帮忙 主要活动:Java Android自动短信回复电话不工作,java,android,string,android-studio,Java,Android,String,Android Studio,我已经创建了一个应用程序,可以启动广播接收器类,该类应用程序可以通过短信自动响应电话呼叫。接收器正在工作,但出现以下异常: java.lang.IllegalArgumentException:无效的destinationAddress 请帮忙 主要活动: package com.biapps.makin_biscuits; import android.content.BroadcastReceiver; import android.content.Context; import
package com.biapps.makin_biscuits;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.ContextWrapper;
import android.media.AudioManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.ImageButton;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.biapps.Contact;
import com.biapps.DatabaseHandler;
import java.util.List;
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();
}
}
};
package com.biapps.makin_biscuits;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
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 + "");
String PhoneNr = incomingNumber;
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Toast.makeText(mContext,"no-one Calling " + PhoneNr,Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
// -- check international call or not.
if (incomingNumber.startsWith("1865")) {
Toast.makeText(mContext,"Knoxville Calling " + PhoneNr + "",Toast.LENGTH_LONG).show();
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(PhoneNr, null, "I'm busy bruh", null, null);
} else {
Toast.makeText(mContext,"Other Calling: " + PhoneNr + "", Toast.LENGTH_LONG).show();
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(PhoneNr, null, "I'm busy bruh", null, null);
}
}
}
};
}
<?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>
来电收信人:
package com.biapps.makin_biscuits;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.ContextWrapper;
import android.media.AudioManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.ImageButton;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.biapps.Contact;
import com.biapps.DatabaseHandler;
import java.util.List;
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();
}
}
};
package com.biapps.makin_biscuits;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
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 + "");
String PhoneNr = incomingNumber;
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Toast.makeText(mContext,"no-one Calling " + PhoneNr,Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
// -- check international call or not.
if (incomingNumber.startsWith("1865")) {
Toast.makeText(mContext,"Knoxville Calling " + PhoneNr + "",Toast.LENGTH_LONG).show();
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(PhoneNr, null, "I'm busy bruh", null, null);
} else {
Toast.makeText(mContext,"Other Calling: " + PhoneNr + "", Toast.LENGTH_LONG).show();
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(PhoneNr, null, "I'm busy bruh", null, null);
}
}
}
};
}
<?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>
清单:
package com.biapps.makin_biscuits;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.ContextWrapper;
import android.media.AudioManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.ImageButton;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.biapps.Contact;
import com.biapps.DatabaseHandler;
import java.util.List;
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();
}
}
};
package com.biapps.makin_biscuits;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
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 + "");
String PhoneNr = incomingNumber;
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Toast.makeText(mContext,"no-one Calling " + PhoneNr,Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
// -- check international call or not.
if (incomingNumber.startsWith("1865")) {
Toast.makeText(mContext,"Knoxville Calling " + PhoneNr + "",Toast.LENGTH_LONG).show();
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(PhoneNr, null, "I'm busy bruh", null, null);
} else {
Toast.makeText(mContext,"Other Calling: " + PhoneNr + "", Toast.LENGTH_LONG).show();
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(PhoneNr, null, "I'm busy bruh", null, null);
}
}
}
};
}
<?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>
回答我自己的问题。问题在于使用incomingNumber分配字符串PhoneNr。删除该步骤可以解决问题。回答我自己的问题。问题在于使用incomingNumber分配字符串PhoneNr。删除该步骤可以解决问题。“我猜“incomingNumber”实际上没有被检索到。”-无需猜测。读你的日志。您有一个带有编号的日志打印:log.i(标记,“编号:+incomingNumber+”)事实上,你是对的。上面写着:04-18 19:45:42.989 13915-13915/com.biapps.Diive I/MyListener:number:这是否意味着我没有得到@MikeM.的号码。?知道它为什么不起作用吗?“我猜“incomingNumber”实际上没有被检索到。”-没必要猜。读你的日志。您有一个带有编号的日志打印:log.i(标记,“编号:+incomingNumber+”)事实上,你是对的。上面写着:04-18 19:45:42.989 13915-13915/com.biapps.Diive I/MyListener:number:这是否意味着我没有得到@MikeM.的号码。?知道它为什么不起作用吗?