当有人拨打电话时,如果我拒绝或放弃呼叫,我的应用程序会向呼叫者发送自动回复。但我提出问题,当我拒绝呼叫时,AVD会显示一条消息 - “很遗憾Autoreply2Caller已停止” 。请帮助。这是我的源代码: package bd.com.example.autoreply2caller;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.telephony.TelephonyManager;
import android.telephony.gsm.SmsManager;
import android.widget.Toast;
public class ReplyToCaller extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Toast.makeText(context, "SMS Received: " + intent.getAction(),
// Toast.LENGTH_LONG).show();
if (intent.getAction()
.equals("android.provider.Telephony.SMS_RECEIVED")) {
// extract the SMS
Bundle bundle = intent.getExtras();
SmsMessage[] chunks = null;
String number = "";
String text = "";
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
chunks = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
chunks[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
text += chunks[i].getMessageBody();
number = chunks[i].getOriginatingAddress();
}
}
Toast.makeText(context,
"SMS Received: from: " + number + " Message Body: " + text,
Toast.LENGTH_LONG).show();
// send an automatic reply
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(number, null,
"I am busy right now. I'll call you later.", null, null);
}
// reply to the caller
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
// Phone is ringing
} else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
// Call received
} else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
// Call Dropped or rejected
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
// send an automatic reply
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(number, null,
"I am busy right now. I'll call you later.", null, null);
}
}
}
清单
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="bd.com.example.autoreply2caller"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.CALL_PRIVILEGED"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<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>
<receiver android:name="ReplyToCaller">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
<action android:name="android.intent.action.PHONE_STATE"/>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
</application>
</manifest>
11-18 20:05:30.543: D/AndroidRuntime(3311): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
11-18 20:05:30.543: D/AndroidRuntime(3311): CheckJNI is ON
11-18 20:05:30.613: D/dalvikvm(3311): Trying to load lib libjavacore.so 0x0
11-18 20:05:30.623: D/dalvikvm(3311): Added shared lib libjavacore.so 0x0
11-18 20:05:30.709: D/dalvikvm(3311): Trying to load lib libnativehelper.so 0x0
11-18 20:05:30.709: D/dalvikvm(3311): Added shared lib libnativehelper.so 0x0
11-18 20:05:31.899: D/AndroidRuntime(3311): Calling main entry com.android.commands.am.Am
11-18 20:05:31.963: I/ActivityManager(1022): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=bd.com.example.autoreply2caller/.MainActivity u=0} from pid 3311
11-18 20:05:31.963: W/WindowManager(1022): Failure taking screenshot for (164x246) to layer 21030
11-18 20:05:32.093: D/AndroidRuntime(3311): Shutting down VM
11-18 20:05:32.093: D/dalvikvm(798): WAIT_FOR_CONCURRENT_GC blocked 0ms
11-18 20:05:32.103: I/ActivityManager(1022): Start proc bd.com.example.autoreply2caller for activity bd.com.example.autoreply2caller/.MainActivity: pid=3321 uid=10053 gids={1028}
11-18 20:05:32.193: D/dalvikvm(3321): Not late-enabling CheckJNI (already on)
11-18 20:05:32.247: I/Choreographer(1022): Skipped 40 frames! The application may be doing too much work on its main thread.
11-18 20:05:32.293: D/dalvikvm(3321): Debugger has detached; object registry had 1 entries
11-18 20:05:32.303: D/dalvikvm(3311): GC_CONCURRENT freed 101K, 77% free 479K/2048K, paused 4ms+36ms, total 163ms
11-18 20:05:32.303: D/jdwp(3311): Got wake-up signal, bailing out of select
11-18 20:05:32.303: D/dalvikvm(3311): Debugger has detached; object registry had 1 entries
11-18 20:05:32.393: D/dalvikvm(798): GC_EXPLICIT freed 37K, 6% free 5935K/6275K, paused 4ms+6ms, total 272ms
11-18 20:05:32.393: D/dalvikvm(798): WAIT_FOR_CONCURRENT_GC blocked 0ms
11-18 20:05:32.413: E/Trace(3321): error opening trace file: No such file or directory (2)
11-18 20:05:32.533: D/dalvikvm(798): GC_EXPLICIT freed <1K, 6% free 5935K/6275K, paused 45ms+5ms, total 139ms
11-18 20:05:32.533: D/dalvikvm(798): WAIT_FOR_CONCURRENT_GC blocked 0ms
11-18 20:05:32.683: D/dalvikvm(1022): WAIT_FOR_CONCURRENT_GC blocked 0ms
11-18 20:05:32.693: D/dalvikvm(798): GC_EXPLICIT freed <1K, 6% free 5935K/6275K, paused 67ms+3ms, total 157ms
11-18 20:05:32.903: I/Choreographer(1022): Skipped 32 frames! The application may be doing too much work on its main thread.
11-18 20:05:33.423: D/dalvikvm(1022): GC_EXPLICIT freed 484K, 20% free 9202K/11399K, paused 15ms+25ms, total 738ms
11-18 20:05:33.523: I/Choreographer(1022): Skipped 33 frames! The application may be doing too much work on its main thread.
11-18 20:05:33.793: D/gralloc_goldfish(3321): Emulator without GPU emulation detected.
11-18 20:05:33.903: I/Choreographer(1022): Skipped 31 frames! The application may be doing too much work on its main thread.
11-18 20:05:33.903: I/ActivityManager(1022): Displayed bd.com.example.autoreply2caller/.MainActivity: +1s854ms
11-18 20:05:33.983: I/Choreographer(1022): Skipped 50 frames! The application may be doing too much work on its main thread.
11-18 20:05:35.492: D/CallNotifier(1119): RINGING... (new)
11-18 20:05:35.492: D/CallNotifier(1119): onNewRingingConnection(): state = RINGING, conn = { incoming: true state: INCOMING post dial state: NOT_STARTED }
11-18 20:05:35.502: D/CallNotifier(1119): SignalInfoTonePlayer.run(toneId = 98)...
11-18 20:05:35.552: D/PhoneNumberUtils(1119): System property doesn't provide any emergency numbers. Use embedded logic for determining ones.
11-18 20:05:35.602: D/PhoneStatusBar(1084): disable: < expand icons ALERTS* ticker system_info back home recent clock >
11-18 20:05:35.642: D/AccelerometerListener(1119): enable(false)
11-18 20:05:35.862: D/CallNotifier(1119): showIncomingCall()... phone state = RINGING
11-18 20:05:35.962: D/dalvikvm(1191): GC_CONCURRENT freed 334K, 9% free 6894K/7495K, paused 16ms+6ms, total 253ms
11-18 20:05:36.063: D/PhoneStatusBar(1084): addNotification score=20
11-18 20:05:36.342: D/PhoneStatusBar(1084): Notification has fullScreenIntent; sending fullScreenIntent
11-18 20:05:36.342: I/ActivityManager(1022): START {act=android.intent.action.MAIN flg=0x10840000 cmp=com.android.phone/.InCallScreen u=0} from pid -1
11-18 20:05:36.392: I/Choreographer(1022): Skipped 32 frames! The application may be doing too much work on its main thread.
11-18 20:05:36.392: W/WindowManager(1022): Failure taking screenshot for (164x246) to layer 21030
11-18 20:05:36.412: E/WVMExtractor(800): Failed to open libwvm.so
11-18 20:05:36.543: I/Choreographer(1084): Skipped 156 frames! The application may be doing too much work on its main thread.
11-18 20:05:36.692: D/Ringtone(1119): Successfully created local player
11-18 20:05:36.692: I/AudioService(1022): AudioFocus requestAudioFocus() from AudioFocus_For_Phone_Ring_And_Calls
11-18 20:05:36.722: D/AudioHardwareInterface(800): setMode(RINGTONE)
11-18 20:05:36.772: I/Choreographer(1084): Skipped 56 frames! The application may be doing too much work on its main thread.
11-18 20:05:36.812: D/AudioSink(800): bufferCount (4) is too small and increased to 12
11-18 20:05:36.822: I/Choreographer(1084): Skipped 32 frames! The application may be doing too much work on its main thread.
11-18 20:05:36.912: D/InCallScreen(1119): onNewIntent: intent = Intent { act=android.intent.action.MAIN flg=0x10c40000 cmp=com.android.phone/.InCallScreen }, phone state = RINGING
11-18 20:05:36.912: D/PhoneStatusBar(1084): disable: < EXPAND* icons ALERTS ticker system_info back home recent clock >
11-18 20:05:37.063: D/InCallTouchUi(1119): showIncomingCallWidget(). widget visibility: 8
11-18 20:05:37.105: I/Choreographer(1022): Skipped 82 frames! The application may be doing too much work on its main thread.
11-18 20:05:37.105: I/Choreographer(1084): Skipped 83 frames! The application may be doing too much work on its main thread.
11-18 20:05:37.122: D/PhoneStatusBar(1084): disable: < EXPAND icons ALERTS ticker system_info BACK* HOME* RECENT* clock >
11-18 20:05:37.162: I/Choreographer(1119): Skipped 144 frames! The application may be doing too much work on its main thread.
11-18 20:05:37.326: I/Choreographer(1084): Skipped 129 frames! The application may be doing too much work on its main thread.
11-18 20:05:37.692: I/Choreographer(1119): Skipped 237 frames! The application may be doing too much work on its main thread.
11-18 20:05:37.732: I/Choreographer(1022): Skipped 64 frames! The application may be doing too much work on its main thread.
11-18 20:05:37.862: I/Choreographer(1022): Skipped 65 frames! The application may be doing too much work on its main thread.
11-18 20:05:38.732: D/InCallTouchUi(1119): showIncomingCallWidget(). widget visibility: 0
11-18 20:05:38.742: I/Choreographer(1119): Skipped 725 frames! The application may be doing too much work on its main thread.
11-18 20:05:38.912: I/Choreographer(1022): Skipped 80 frames! The application may be doing too much work on its main thread.
11-18 20:05:39.142: D/dalvikvm(1310): WAIT_FOR_CONCURRENT_GC blocked 0ms
11-18 20:05:39.172: I/Choreographer(1119): Skipped 249 frames! The application may be doing too much work on its main thread.
11-18 20:05:39.462: D/dalvikvm(1310): GC_EXPLICIT freed 296K, 12% free 8404K/9479K, paused 4ms+54ms, total 325ms
11-18 20:05:39.692: I/Choreographer(1119): Skipped 317 frames! The application may be doing too much work on its main thread.
11-18 20:05:39.842: I/Choreographer(1119): Skipped 100 frames! The application may be doing too much work on its main thread.
11-18 20:05:40.062: I/Choreographer(1119): Skipped 143 frames! The application may be doing too much work on its main thread.
11-18 20:05:40.372: I/Choreographer(1119): Skipped 205 frames! The application may be doing too much work on its main thread.
11-18 20:05:40.672: I/Choreographer(1119): Skipped 193 frames! The application may be doing too much work on its main thread.
11-18 20:05:40.902: I/Choreographer(1119): Skipped 153 frames! The application may be doing too much work on its main thread.
11-18 20:05:41.192: I/Choreographer(1119): Skipped 187 frames! The application may be doing too much work on its main thread.
11-18 20:05:41.304: D/dalvikvm(1119): GC_CONCURRENT freed 388K, 9% free 7589K/8327K, paused 47ms+11ms, total 189ms
11-18 20:05:41.432: I/Choreographer(1119): Skipped 156 frames! The application may be doing too much work on its main thread.
11-18 20:05:41.672: I/Choreographer(1119): Skipped 157 frames! The application may be doing too much work on its main thread.
11-18 20:05:41.972: I/Choreographer(1119): Skipped 191 frames! The application may be doing too much work on its main thread.
11-18 20:05:42.292: D/PhoneUtils(1119): hangup(): hanging up ringing call
11-18 20:05:42.292: D/PhoneUtils(1119): hangupRingingCall(): regular incoming call: hangup()
11-18 20:05:42.292: D/PhoneUtils(1119): - hangup(Call): regular hangup()...
11-18 20:05:42.332: I/Choreographer(1119): Skipped 235 frames! The application may be doing too much work on its main thread.
11-18 20:05:42.622: I/Choreographer(1119): Skipped 225 frames! The application may be doing too much work on its main thread.
11-18 20:05:42.732: I/Choreographer(1119): Skipped 70 frames! The application may be doing too much work on its main thread.
11-18 20:05:42.852: I/Choreographer(1119): Skipped 83 frames! The application may be doing too much work on its main thread.
11-18 20:05:43.122: D/PhoneStatusBar(1084): disable: < EXPAND icons alerts* ticker system_info BACK HOME RECENT clock >
11-18 20:05:43.174: D/AndroidRuntime(3321): Shutting down VM
11-18 20:05:43.174: W/dalvikvm(3321): threadid=1: thread exiting with uncaught exception (group=0xb3de7288)
11-18 20:05:43.192: E/AndroidRuntime(3321): FATAL EXCEPTION: main
11-18 20:05:43.192: E/AndroidRuntime(3321): java.lang.RuntimeException: Unable to start receiver bd.com.example.autoreply2caller.ReplyToCaller: java.lang.IllegalArgumentException: Invalid destinationAddress
11-18 20:05:43.192: E/AndroidRuntime(3321): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2236)
11-18 20:05:43.192: E/AndroidRuntime(3321): at android.app.ActivityThread.access$1500(ActivityThread.java:130)
11-18 20:05:43.192: E/AndroidRuntime(3321): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1271)
11-18 20:05:43.192: E/AndroidRuntime(3321): at android.os.Handler.dispatchMessage(Handler.java:99)
11-18 20:05:43.192: E/AndroidRuntime(3321): at android.os.Looper.loop(Looper.java:137)
11-18 20:05:43.192: E/AndroidRuntime(3321): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-18 20:05:43.192: E/AndroidRuntime(3321): at java.lang.reflect.Method.invokeNative(Native Method)
11-18 20:05:43.192: E/AndroidRuntime(3321): at java.lang.reflect.Method.invoke(Method.java:511)
11-18 20:05:43.192: E/AndroidRuntime(3321): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-18 20:05:43.192: E/AndroidRuntime(3321): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-18 20:05:43.192: E/AndroidRuntime(3321): at dalvik.system.NativeStart.main(Native Method)
11-18 20:05:43.192: E/AndroidRuntime(3321): Caused by: java.lang.IllegalArgumentException: Invalid destinationAddress
11-18 20:05:43.192: E/AndroidRuntime(3321): at android.telephony.SmsManager.sendTextMessage(SmsManager.java:77)
11-18 20:05:43.192: E/AndroidRuntime(3321): at android.telephony.gsm.SmsManager.sendTextMessage(SmsManager.java:79)
11-18 20:05:43.192: E/AndroidRuntime(3321): at bd.com.example.autoreply2caller.ReplyToCaller.onReceive(ReplyToCaller.java:74)
11-18 20:05:43.192: E/AndroidRuntime(3321): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2229)
11-18 20:05:43.192: E/AndroidRuntime(3321): ... 10 more
11-18 20:05:43.202: D/AccelerometerListener(1119): enable(false)
11-18 20:05:43.252: D/PhoneStatusBar(1084): disable: < expand* icons alerts ticker system_info BACK HOME RECENT clock >
11-18 20:05:43.282: I/Choreographer(1119): Skipped 273 frames! The application may be doing too much work on its main thread.
11-18 20:05:43.613: I/Choreographer(1022): Skipped 90 frames! The application may be doing too much work on its main thread.
11-18 20:05:43.922: I/Choreographer(1022): Skipped 33 frames! The application may be doing too much work on its main thread.
11-18 20:05:43.962: D/InCallTouchUi(1119): hideIncomingCallWidget()
11-18 20:05:43.972: D/PhoneStatusBar(1084): disable: < expand icons alerts ticker system_info back* home* recent* clock >
11-18 20:05:43.972: I/Choreographer(1119): Skipped 441 frames! The application may be doing too much work on its main thread.
11-18 20:05:44.202: I/Choreographer(1022): Skipped 34 frames! The application may be doing too much work on its main thread.
11-18 20:05:44.542: D/CallNotifier(1119): onDisconnect: cause = INCOMING_REJECTED, incoming = true, date = 1353247535489
11-18 20:05:44.542: D/CallNotifier(1119): SignalInfoTonePlayer.run(toneId = 98)...
11-18 20:05:44.602: D/AudioHardwareInterface(800): setMode(NORMAL)
11-18 20:05:44.642: I/AudioService(1022): AudioFocus abandonAudioFocus() from AudioFocus_For_Phone_Ring_And_Calls
11-18 20:05:44.692: I/PowerManagerService(1022): clearUserActivity for 10000ms from now
11-18 20:05:44.702: D/PhoneNumberUtils(1119): System property doesn't provide any emergency numbers. Use embedded logic for determining ones.
11-18 20:05:44.713: I/ActivityManager(1022): moveTaskToBack: 6
11-18 20:05:45.002: W/WindowManager(1022): Failure taking screenshot for (164x246) to layer 21035
11-18 20:05:45.092: I/Choreographer(1119): Skipped 723 frames! The application may be doing too much work on its main thread.
11-18 20:05:45.182: I/Choreographer(1022): Skipped 98 frames! The application may be doing too much work on its main thread.
11-18 20:05:45.443: I/Choreographer(1119): Skipped 228 frames! The application may be doing too much work on its main thread.
11-18 20:05:45.573: I/Choreographer(1119): Skipped 71 frames! The application may be doing too much work on its main thread.
11-18 20:05:45.747: D/InCallTouchUi(1119): - triggerPing: InCallScreen no longer in foreground; ignoring...
11-18 20:05:46.454: W/ActivityManager(1022): Launch timeout has expired, giving up wake lock!
11-18 20:05:50.532: I/Choreographer(1022): Skipped 33 frames! The application may be doing too much work on its main thread.
11-18 20:05:55.350: W/ActivityManager(1022): Activity idle timeout for ActivityRecord{b471e7b0 bd.com.example.autoreply2caller/.MainActivity}
答案 0 :(得分:0)
要收听手机状态,请使用PhoneStateListener
查看:this answer
尝试smsMessages[n].getDisplayOriginatingAddress()
我总是从短信中提取地址:
Object messages[] = (Object[]) bundle.get("pdus");
smsMessages = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++)
{
smsMessages[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
JLog.v("Reciever: SMS :-\r\n ========== \r\n "
+ smsMessages[n].getDisplayOriginatingAddress() + "\r\n "
+ smsMessages[n].getServiceCenterAddress() + "\r\n "
+ smsMessages[n].getDisplayMessageBody()
+ "\r\n========");
}
重新发送短信时,您可能需要做的就是确保数字格式化!真正记录这些东西有帮助!
外拨电话号码需要看起来像4412345123456
或任何其他有效号码。
PhoneNumberUtils有格式化方法。