2014-01-28 57 views
0

另一个活动我试图调用方法[方法名被验证()]从另一个活动接收特定字(随机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

你在AndroidManifest.xml中设置了接收器吗? – Sundeep

+0

我确实收到短信并成功执行了代码,并成功执行了Toast在短信中可见接收...我将发布我的AndroidManifest.xml ...请检查我是否缺少somthing ...谢谢 – user3244365

+0

检查我的答案。无论如何,你的代码并不遵循Java命名约定和格式。我建议你清理你的代码并投入一些Java基础知识。 – Sundeep

回答

0

要开始一项活动,您应该使用Context.startActivity(intent)。查看完整的代码如下

Intent intent = new Intent(context,mainpageactivity.class); 
context.startActivity(intent); 
+0

thsnks为您的答复。名为验证方法不呼叫...我正确的下面的代码? 如果(message.equals(vcodei)){ INT durationge = Toast.LENGTH_LONG; Toast toastge =烤面包。makeText(上下文,“已验证”,durationge); toastge.show(); //到这里的代码可以正确执行 mainpageactivity MP =新mainpageactivity(){ 公共无效启动(){ this.verifyed(); //调用此方法的问题 } }; – user3244365

+0

该代码是错误的。在'toastge.show()'后面,放上面两行并删除剩下的部分 – Sundeep

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); 

然后在你的活动,你定义你的接收器和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:

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的,你不需要它了。 我希望它能为你工作。