我试图在接收特定单词的短信(随机10个后者)时从另一个活动调用方法[方法名称验证()],该单词存储在名为: - vcode的字符串中。
public class IncomingSms extends BroadcastReceiver {
protected LocationManager locationManager;
public void onReceive(Context context, Intent intent) {
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
if (message.equals(vcodei)) {
int durationge = Toast.LENGTH_LONG;
Toast toastge = Toast.makeText(context, "Verifyed ", durationge);
toastge.show();
mainpageactivity mp = new mainpageactivity() {
public void launch() {
this.verifyed();
}
};
}
}
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
int durationg = Toast.LENGTH_LONG;
Toast toastg = Toast.makeText(context,"map error"+ e , durationg);
toastg.show();
}
}
}
我的第二个活动如下,我提到了导入,需要调用该方法......
package com.androidexample.broadcastreceiver;
import com.androidexample.broadcastreceiver.IncomingSms;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.telephony.SmsMessage;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import static com.androidexample.broadcastreceiver.BroadcastNewSms.buffer;
import static com.androidexample.broadcastreceiver.IncomingSms.num_buffer;
import static com.androidexample.broadcastreceiver.IncomingSms.vcodei;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;
public class mainpageactivity extends Activity {
public String mobile;
public String vcode;
private String log;
private Button register_btn;
private EditText countryCode_txt;
private EditText mobileno_txt;
private TextView note_txt;
public EditText name_txt;
private TextView textView1;
private TextView textView2;
private TextView textView3;
private TextView textView4;
private TextView textView5;
DatabaseHandler db = new DatabaseHandler(this);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.mainpage);
register_btn= (Button) findViewById(R.id.register_btn);
countryCode_txt =(EditText) findViewById(R.id.countryCode_txt);
mobileno_txt =(EditText) findViewById(R.id.mobileno_txt);
note_txt =(TextView) findViewById(R.id.note_txt);
textView1 =(TextView) findViewById(R.id.textView1);
textView2 =(TextView) findViewById(R.id.textView2);
textView3 =(TextView) findViewById(R.id.textView3);
textView4 =(TextView) findViewById(R.id.textView4);
textView5 =(TextView) findViewById(R.id.textView5);
name_txt =(EditText) findViewById(R.id.name_txt);
public void verifyed() {
db.addContact(new Contact(name_txt.getText().toString().trim(),mobile));
Toast.makeText(getApplicationContext(), "Now what..",
Toast.LENGTH_LONG).show();
Intent register = new Intent(mainpageactivity.this, BroadcastNewSms.class);
startActivity(register);
mainpageactivity.this.finish();
}
}
AndroidManifest.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidexample.broadcastreceiver"
android:versionCode="1"
android:versionName="1.0" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.androidexample.broadcastreceiver.mainpageactivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.androidexample.broadcastreceiver.IncomingSms">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<activity android:name="BroadcastNewSms" ></activity>
</application>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
请回复您的宝贵帮助... 我是android的新手..
由于
答案 0 :(得分:0)
要开始活动,您应该使用Context.startActivity(intent)
。请参阅下面的完整代码
Intent intent = new Intent(context,mainpageactivity.class);
context.startActivity(intent);
答案 1 :(得分:0)
您保持广播接收器不变,并在其中添加广播意图,而不是:
mainpageactivity mp = new mainpageactivity() {
public void launch() {
this.verifyed();
}
你删除了那个并且你输入了这段代码:
// this intent will call the activity whih will lunch your methode
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("com.appname.SMS_RECU");
context.sendBroadcast(broadcastIntent);
然后在您的活动中,您可以在活动定义之后直接定义您的receiver和intentFilter:
SmsReceiver broadcastReceiver;
IntentFilter intentFilter = new IntentFilter();
然后在onCreat中实例化你的接收器:
broadcastReceiver = new SmsReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
/*you call your methode*/
verifyed();
}
};
registerReceiver(broadcastReceiver, intentFilter);
你还创建了一个intentFilter,它将由onCreat中的broadcastReceiver调用:
intentFilter.addAction("com.appname.SMS_RECU");
你在onResume注册你的接收者:
@Override
protected void onResume() {
super.onResume();
registerReceiver(broadcastReceiver, intentFilter);
}
你取消注册你的接收者,在onPause:
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(broadcastReceiver);
}
删除您在manifest.xml中添加的接收者和intentFilter,您不再需要它了。 我希望它对你有用。