开始从SMS广播reciever一个活动时接收从一个特定的数字信息 [英] starting an activity from sms broadcast reciever when message from a particular number is received
问题描述
我跟了很多联系,但还是没能找到解决我的问题。基本上,我有2个班 UsingBroadcastReceiver
和 StartAction
。当我收到一条消息,从一个特定的号码,我需要启动 StartAction
的活动。对于这一点,我已经实现了以下code。
更新code: -
公共类UsingBroadcastReceiver扩展的BroadcastReceiver {
最后SmsManager的短信= SmsManager.getDefault();
串体=无效;
字符串否=15555215558;
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
//解析短信。
捆绑包= intent.getExtras();
SmsMessage []封邮件= NULL;
字符串str =;
如果(捆绑!= NULL)
{
//获得的短信。
[对象]的PDU =(对象[])bundle.get(的PDU);
封邮件=新SmsMessage [pdus.length]
的for(int i = 0; I< msgs.length;我++)
{
封邮件[I] = SmsMessage.createFromPdu((字节[])的PDU [I]);
如果(消息[I] .getOriginatingAddress()。等于(无))
{
身体=封邮件[I] .getMessageBody();
STR + =短信来自+封邮件[I] .getOriginatingAddress();
STR + =;
STR + =封邮件[I] .getMessageBody()的toString()。
STR + =\ N的;
意图打开=新的意图(背景下,StartAction.class);
open.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
open.putExtra(身体,身体);
context.startActivity(开);
}
}
//显示短信的吐司。
Toast.makeText(背景下,海峡,Toast.LENGTH_SHORT).show();
}
}
}
当我运行在模拟器上这个code,以及设备,我得到的 ForceClose
的错误消息。
的logcat
如下:
11月2日至二十六号:40:50.047:E / AndroidRuntime(689):致命异常:主要
十一月二号至26日:40:50.047:E / AndroidRuntime(689):java.lang.RuntimeException的:无法实例活动ComponentInfo{com.example.usingbroadcastreceiver/com.example.usingbroadcastreceiver.UsingBroadcastReceiver}: java.lang.ClassCastException:com.example.usingbroadcastreceiver.UsingBroadcastReceiver
十一月二号至26日:40:50.047:E / AndroidRuntime(689):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
十一月二号至26日:40:50.047:E / AndroidRuntime(689):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
十一月二号至26日:40:50.047:E / AndroidRuntime(689):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
十一月二号至26日:40:50.047:E / AndroidRuntime(689):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
十一月二号至26日:40:50.047:E / AndroidRuntime(689):在android.os.Handler.dispatchMessage(Handler.java:99)
十一月二号至26日:40:50.047:E / AndroidRuntime(689):在android.os.Looper.loop(Looper.java:123)
十一月二号至26日:40:50.047:E / AndroidRuntime(689):在android.app.ActivityThread.main(ActivityThread.java:3683)
十一月二号至26日:40:50.047:E / AndroidRuntime(689):在java.lang.reflect.Method.invokeNative(本机方法)
十一月二号至26日:40:50.047:E / AndroidRuntime(689):在java.lang.reflect.Method.invoke(Method.java:507)
十一月二号至26日:40:50.047:E / AndroidRuntime(689):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
十一月二号至26日:40:50.047:E / AndroidRuntime(689):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
十一月二号至26日:40:50.047:E / AndroidRuntime(689):在dalvik.system.NativeStart.main(本机方法)
十一月二号至26日:40:50.047:E / AndroidRuntime(689):java.lang.ClassCastException:产生的原因com.example.usingbroadcastreceiver.UsingBroadcastReceiver
十一月二号至26日:40:50.047:E / AndroidRuntime(689):在android.app.Instrumentation.newActivity(Instrumentation.java:1021)
十一月二号至26日:40:50.047:E / AndroidRuntime(689):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
十一月二号至26日:40:50.047:E / AndroidRuntime(689):11 ...更多
另外,我需要访问的 StartAction
类 UsingBroadcastReceiver
类的字符串体的价值。这是做了正确的方法是什么?
StartAction开始=新StartAction();
start.msgBody =身体;
我的清单如下:
< XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.example.usingbroadcastreceiver
安卓版code =1
机器人:VERSIONNAME =1.0>
<用途-SDK
安卓的minSdkVersion =8
机器人:targetSdkVersion =17/>
<使用-权限的Android:名称=android.permission.INTERNET对/>
<使用-权限的Android:名称=android.permission.RECEIVE_SMS/>
<使用-权限的Android:名称=android.permission.READ_SMS/>
<应用
机器人:allowBackup =真
机器人:图标=@可绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme>
<活动
机器人:名称=com.example.usingbroadcastreceiver.UsingBroadcastReceiver
机器人:标签=@字符串/ APP_NAME>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<活动
机器人:名称=com.example.usingbroadcastreceiver.StartAction
机器人:标签=@字符串/ APP_NAME>
<意向滤光器>
<作用机器人:名称=com.example.usingbroadcastreceiver.StartAction/>
<类机器人:名称=android.intent.category.DEFAULT/>
&所述; /意图滤光器>
< /活性GT;
<接收器的Android版本:NAME =UsingBroadcastReceiver>
<意向滤光器>
<作用机器人:名称=android.provider.Telephony.SMS_RECEIVED>< /作用>
&所述; /意图滤光器>
< /接收器>
< /用途>
< /舱单>
<接收机器人:名称=。UsingBroadcastReceiver>
你不插入。在接收器
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
字符串地址1 = NULL;
捆绑包= intent.getExtras();
如果(捆绑!= NULL){
[对象]的PDU =(对象[])bundle.get(的PDU);
SmsMessage []消息=新SmsMessage [pdus.length]
的for(int i = 0; I< pdus.length;我++){
消息[I] = SmsMessage.createFromPdu((字节[])的PDU [I]);
地址1 =消息[I] .getOriginatingAddress();
}
如果(地址1 ==你compareable号){
意图打开=新的意图(背景下,StartAction.class);
open.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(开);
}
} }
I have followed many links but still could not find a solution to my problem. Basically, i have 2 classes UsingBroadcastReceiver
and StartAction
. When i receive a message from a particular number, i need to start the StartAction
activity. For this, i have implemented the following code.
Updated code:-
public class UsingBroadcastReceiver extends BroadcastReceiver {
final SmsManager sms = SmsManager.getDefault();
String body = null;
String no = "15555215558";
@Override
public void onReceive(Context context, Intent intent) {
// Parse the SMS.
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
// Retrieve the SMS.
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]);
if(msgs[i].getOriginatingAddress().equals(no))
{
body = msgs[i].getMessageBody();
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
Intent open = new Intent(context, StartAction.class);
open.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
open.putExtra("body", body);
context.startActivity(open);
}
}
// Display the SMS as Toast.
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
}
When i run this code on the emulator as well as the device, i get the ForceClose
error message.
logcat
is as follows:
02-26 11:40:50.047: E/AndroidRuntime(689): FATAL EXCEPTION: main
02-26 11:40:50.047: E/AndroidRuntime(689): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.usingbroadcastreceiver/com.example.usingbroadcastreceiver.UsingBroadcastReceiver}: java.lang.ClassCastException: com.example.usingbroadcastreceiver.UsingBroadcastReceiver
02-26 11:40:50.047: E/AndroidRuntime(689): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
02-26 11:40:50.047: E/AndroidRuntime(689): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-26 11:40:50.047: E/AndroidRuntime(689): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-26 11:40:50.047: E/AndroidRuntime(689): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-26 11:40:50.047: E/AndroidRuntime(689): at android.os.Handler.dispatchMessage(Handler.java:99)
02-26 11:40:50.047: E/AndroidRuntime(689): at android.os.Looper.loop(Looper.java:123)
02-26 11:40:50.047: E/AndroidRuntime(689): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-26 11:40:50.047: E/AndroidRuntime(689): at java.lang.reflect.Method.invokeNative(Native Method)
02-26 11:40:50.047: E/AndroidRuntime(689): at java.lang.reflect.Method.invoke(Method.java:507)
02-26 11:40:50.047: E/AndroidRuntime(689): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-26 11:40:50.047: E/AndroidRuntime(689): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-26 11:40:50.047: E/AndroidRuntime(689): at dalvik.system.NativeStart.main(Native Method)
02-26 11:40:50.047: E/AndroidRuntime(689): Caused by: java.lang.ClassCastException: com.example.usingbroadcastreceiver.UsingBroadcastReceiver
02-26 11:40:50.047: E/AndroidRuntime(689): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-26 11:40:50.047: E/AndroidRuntime(689): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
02-26 11:40:50.047: E/AndroidRuntime(689): ... 11 more
Also, i need to access the value of String body of the UsingBroadcastReceiver
class in the StartAction
class. Is this the correct way to do it?
StartAction start = new StartAction();
start.msgBody = body;
My manifest is as follows:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.usingbroadcastreceiver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.usingbroadcastreceiver.UsingBroadcastReceiver"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.usingbroadcastreceiver.StartAction"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.usingbroadcastreceiver.StartAction" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<receiver android:name="UsingBroadcastReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
</application>
</manifest>
<receiver android:name=".UsingBroadcastReceiver">
you do not insert "." in receiver
@Override
public void onReceive(Context context, Intent intent) {
String address1 = null;
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
address1 = messages[i].getOriginatingAddress();
}
if (address1==your compareable number ){
Intent open = new Intent(context, StartAction.class);
open.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(open);
}
} }
这篇关于开始从SMS广播reciever一个活动时接收从一个特定的数字信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!