【发布时间】:2018-06-19 00:42:39
【问题描述】:
我不想在我的 android 中构建 SMS 触发的应用程序。我知道市场上有很多应用程序可以做到这一点,但我只想自己尝试一下。
我的 SMS 接收器项目和我的 Caller 项目一样有效。我想将它们组合为 1 个应用程序,但我不知道。有人可以帮帮我吗?
这是我从某处获得并测试工作的代码 sn-p..
调用者.java
package pi.redphone;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
public class Caller extends Activity
{
private Context context;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String num = "2217238";
call(num);
}
public void call(String number)
{
try
{
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+number));
startActivity(callIntent);
}
catch (ActivityNotFoundException activityException)
{
Log.e("dialing-example", "Call failed", activityException);
}
}
}
上面代码中的电话号码是硬编码的,安装后会拨打这个号码2217238
SmsReceiver.java
package pi.redphone;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
//SmsMessage msg = null;
String sender = null;
String msgBody = null;
if(bundle != null)
{
Object[] sms = (Object[]) bundle.get("pdus");
for(int i=0; i<sms.length; i++)
{
SmsMessage msg = SmsMessage.createFromPdu((byte[]) sms[i]);
sender = msg.getOriginatingAddress(); //store sender's mobile #
msgBody = msg.getMessageBody(); //msg content
}
Toast.makeText(context, sender, Toast.LENGTH_LONG ).show();
}
}
}
安装后app会报这个错误
呼叫转移应用程序意外停止工作。请 再试一次。。
实际上我想做的是当有人向我的 android 发送短信时,我的 SmsReceiver 将提取发件人手机号码并回拨.. 发件人的号码存储在 sender 变量中..
我想做这样的事情
call(sender);
但这并不容易..我可以合并源代码而没有错误,但安装后我无法让它工作......
如果我能让它工作的话,这将是我的第一个 Android 应用程序.. ;)
这是我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pi.redphone"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".callForward"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是我的 LogCAt:
01-15 03:11:50.265: I/jdwp(289): Ignoring second debugger -- accepting and dropping
01-15 03:12:30.756: W/KeyCharacterMap(289): No keyboard for id 0
01-15 03:12:30.756: W/KeyCharacterMap(289): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
01-15 03:13:05.095: D/dalvikvm(289): GC_EXPLICIT freed 1347 objects / 90832 bytes in 74ms
01-15 03:18:25.995: D/AndroidRuntime(357): Shutting down VM
01-15 03:18:26.045: W/dalvikvm(357): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-15 03:18:26.115: E/AndroidRuntime(357): FATAL EXCEPTION: main
01-15 03:18:26.115: E/AndroidRuntime(357): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{pi.redphone/pi.redphone.callForward}: java.lang.ClassCastException: pi.redphone.callForward
01-15 03:18:26.115: E/AndroidRuntime(357): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
01-15 03:18:26.115: E/AndroidRuntime(357): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-15 03:18:26.115: E/AndroidRuntime(357): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-15 03:18:26.115: E/AndroidRuntime(357): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-15 03:18:26.115: E/AndroidRuntime(357): at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 03:18:26.115: E/AndroidRuntime(357): at android.os.Looper.loop(Looper.java:123)
01-15 03:18:26.115: E/AndroidRuntime(357): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-15 03:18:26.115: E/AndroidRuntime(357): at java.lang.reflect.Method.invokeNative(Native Method)
01-15 03:18:26.115: E/AndroidRuntime(357): at java.lang.reflect.Method.invoke(Method.java:521)
01-15 03:18:26.115: E/AndroidRuntime(357): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-15 03:18:26.115: E/AndroidRuntime(357): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-15 03:18:26.115: E/AndroidRuntime(357): at dalvik.system.NativeStart.main(Native Method)
01-15 03:18:26.115: E/AndroidRuntime(357): Caused by: java.lang.ClassCastException: pi.redphone.callForward
01-15 03:18:26.115: E/AndroidRuntime(357): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
01-15 03:18:26.115: E/AndroidRuntime(357): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
01-15 03:18:26.115: E/AndroidRuntime(357): ... 11 more
01-15 03:18:35.085: I/Process(357): Sending signal. PID: 357 SIG: 9
01-15 03:31:01.475: D/AndroidRuntime(449): Shutting down VM
01-15 03:31:01.505: W/dalvikvm(449): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-15 03:31:01.555: E/AndroidRuntime(449): FATAL EXCEPTION: main
01-15 03:31:01.555: E/AndroidRuntime(449): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{pi.redphone/pi.redphone.callForward}: java.lang.ClassCastException: pi.redphone.callForward
01-15 03:31:01.555: E/AndroidRuntime(449): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
01-15 03:31:01.555: E/AndroidRuntime(449): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-15 03:31:01.555: E/AndroidRuntime(449): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-15 03:31:01.555: E/AndroidRuntime(449): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-15 03:31:01.555: E/AndroidRuntime(449): at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 03:31:01.555: E/AndroidRuntime(449): at android.os.Looper.loop(Looper.java:123)
01-15 03:31:01.555: E/AndroidRuntime(449): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-15 03:31:01.555: E/AndroidRuntime(449): at java.lang.reflect.Method.invokeNative(Native Method)
01-15 03:31:01.555: E/AndroidRuntime(449): at java.lang.reflect.Method.invoke(Method.java:521)
01-15 03:31:01.555: E/AndroidRuntime(449): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-15 03:31:01.555: E/AndroidRuntime(449): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-15 03:31:01.555: E/AndroidRuntime(449): at dalvik.system.NativeStart.main(Native Method)
01-15 03:31:01.555: E/AndroidRuntime(449): Caused by: java.lang.ClassCastException: pi.redphone.callForward
01-15 03:31:01.555: E/AndroidRuntime(449): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
01-15 03:31:01.555: E/AndroidRuntime(449): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
01-15 03:31:01.555: E/AndroidRuntime(449): ... 11 more
01-15 03:31:15.255: I/Process(449): Sending signal. PID: 449 SIG: 9
应用程序仍然没有运行。这是我的全部源代码。
package pi.redphone;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class callForward extends BroadcastReceiver
{
/** Called when the activity is first created. */
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
//SmsMessage msg = null;
String sender = null;
String msgBody = null;
if(bundle != null)
{
Object[] sms = (Object[]) bundle.get("pdus");
for(int i=0; i<sms.length; i++)
{
SmsMessage msg = SmsMessage.createFromPdu((byte[]) sms[i]);
sender = msg.getOriginatingAddress(); //store sender's mobile #
msgBody = msg.getMessageBody(); //msg content
}
Toast.makeText(context, sender, Toast.LENGTH_LONG ).show();
}
}
}
我感觉代码是正确的,但我不知道如何解决这个问题..
我不知道如何遵循您的建议,因为我总是遇到错误。而是帮我检查我是否做得对..
这是我最新的代码。通话正常,但我的短信接收器没有。
我不知道怎么称呼我的
public class SmsReceiver extends BroadcastReceiver
在活动中,以便它监听传入的短信。
Call4wardActivity.Java
package pi.redphone;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
public class Call4wardActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
call();
}
public void call()
{
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:123456789"));
startActivity(callIntent);
} catch (ActivityNotFoundException activityException) {
Log.e("helloandroid dialing example", "Call failed", null);
}
}
public class SmsReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
// TODO Auto-generated method stub
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
// String body = null;
String sender = null;
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++)
{
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
sender = msgs[i].getOriginatingAddress();
}
//---display the new SMS message---
Toast.makeText(context, sender, Toast.LENGTH_LONG).show();
}
}
}
}
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pi.redphone"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Call4wardActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.CALL_PHONE"/>
</manifest>
【问题讨论】:
标签: android phone-call forwarding