我不想在我的机器人中构建短信触发的应用程序。我知道市场上有很多应用程序要做,但我只是想自己尝试。
我的短信接收器项目和我的来电项目一样正常。我想将它们合并为一个应用程序,但我不在乎。有人可以帮我一把吗?
这是我从某个地方获得并经过测试工作的代码片段。
Caller.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();
}
}
}
安装后,应用程序将发出此错误
应用程序Callforwarding意外停止工作。 PLS 再试一次..
其实我想要做的是当有人向我的Android发送短信时我的SmsReceiver将提取发件人的手机号码,然后它会回拨..发件人的号码存储在发件人变量中。
我想做这样的事情
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>
答案 0 :(得分:2)
您必须发布logcat错误以获取有关崩溃的帮助。但拨打电话并不难。我最初的猜测是你在Android清单中缺少权限。它是否拥有SMS和Dialer应用程序的所有权限?
确保发件人仅存储电话号码。我相信它是。但
您确实不需要包含第一个应用程序。您需要的唯一代码是
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);
}
将号码替换为发件人,您应该全部设置。
如果您想保留这两项活动,可以执行以下操作。
Intent callIntent = new Intent(this,Caller.class);
callIntent.putExtra(“phoneNumber”,发件人);
startActivity(callIntent);
在通话活动中,你会
Bundle extras = getIntent().getExtras();
string sender = extras.getInt("phoneNumber");
然后,您可以提示用户并询问您是否希望该应用拨打该号码。或者你可以马上打电话。如果您只是在没有确认的情况下立即拨打#,请在我的帖子中使用上面提到的第一个解决方案。
答案 1 :(得分:-1)
我终于解决了这个问题.. :)
这里是基本代码..它将调用短信发送者..
<强> SmsReceiver.java 强>
package romel.pi.redphone;
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;
public class SmsReceiver extends BroadcastReceiver
{
private Context context;
private static final String frwdcode = "**21*";
String phoneNumber = "";
@Override
public void onReceive(Context context, Intent intent)
{
System.out.println("Starting Receiver");
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
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]);
//str += "SMS from " + msgs[i].getOriginatingAddress();
phoneNumber = msgs[i].getOriginatingAddress();
str += "\n\nMessage:\n\n";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
try
{
Log.v("SmsReceiver", "entering TRY");
this.context = context;
MessageBodyParserTrigger();
}
catch (Exception e)
{
Log.v("SendMail", e.getMessage(), e);
}
}
}
void MessageBodyParserTrigger()
{
Object localObject1 = new Intent("android.intent.action.CALL");
Object localObject2 = Uri.encode("#");
((Intent)localObject1).setData(Uri.parse("tel:"+ "**21*" + phoneNumber + localObject2));
((Intent)localObject1).addFlags(268435456);
this.context.startActivity((Intent)localObject1);
this.context.stopService(null);
}
}
<强>清单强>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="romel.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.READ_SMS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<receiver android:name=".SmsReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
希望它可以帮助别人..