-1

好吧,我已经让这个程序接收短信和自动重播,但它接收但不重播这是我的 Reciver class.java

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 Reciving  extends BroadcastReceiver{
static String from;
static String body;

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method s
      //---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]);                
         from =   str +=  "SMS from " + msgs[i].getOriginatingAddress();              
            str += " :";
       body =  str += msgs[i].getMessageBody().toString();
            str += "\n";

        }
        //---display the new SMS message---
        Toast.makeText(context, str, Toast.LENGTH_LONG).show();
       // ret = str;
        SendGlass obj = new SendGlass();
        //   int tos =  Integer.valueOf(body);
         //  obj.Decision(from, tos);

        if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
        {
        Main_Activity obj = new Main_Activity();

       obj.sms(from, body1);

        }
    else{

        }    

    }
}
}

我的短信发送课程

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.telephony.SmsManager;

public class SendGlass extends Activity {
static String REPLAYS1,REPLAYS2;
/** public void databaser()
{
    SQLiteDatabase db = openOrCreateDatabase("TEST", MODE_PRIVATE, null); 
    Cursor c = db.rawQuery("SELECT REPLAYS FROM Rtm WHERE ID = 1 ",null);
    c.moveToFirst();
    REPLAYS1 = c.getString(c.getColumnIndex("REPLAYS"));
    c = db.rawQuery("SELECT REPLAYS FROM Rtm WHERE ID = 2", null);
    c.moveToFirst();
    REPLAYS2 = c.getString(c.getColumnIndex("REPLAYS"));
    c.moveToFirst();
    db.close();
    c.close();

}


public void Decision(String phone ,int mesage )
{
    String phonenum = String.valueOf(mesage); 
    switch(mesage)
    {
    case 1:
        sms(phonenum, "hello world1");
        break;

    case 2:
        sms(phonenum, "hello world2");
        break;
    default:
        sms(phonenum, "defaulter");
        break;
    }


} **/

public void sms (String phone,String Sms )
{


    String phoneNo = phone;
    String sms = Sms;

    SmsManager  smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage(phoneNo, null, sms, null, null);
}



}

我的清单.xml

   <manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="dom.example.dbfinal"
  android:versionCode="1"
   android:versionName="1.0" >

 <uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />


<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".Screen1"
        android:label="@string/title_activity_screen1" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Screen2"
        android:label="@string/title_activity_screen2" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Screen3"
        android:label="@string/title_activity_screen3" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".DtaBase"
        android:label="@string/title_activity_dta_base" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name=".Reciving" >
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" > 
           </action>     
        </intent-filter>
    </receiver>
</application>

谁能告诉我当短信接收但不重播时它显示吐司的问题

4

1 回答 1

0

永远不要做Main_Activity obj = new Main_Activity();Activity 应该只由操作系统处理,创建一个实用程序类。

onRecieve不保证在您的应用程序 UI 线程上尝试:

尝试使用显式设置 Looper 的处理程序:

final SmsManager smsManager = SmsManager.getDefault();
Handler h = new Handler(Looper.getMainLooper());
    h.post(new Runnable()
    {

        @Override
        public void run()
        {
            // Check that Your number is formatted correclty and that your body is LESS than 140 chars!
            smsManager.sendTextMessage("4412345123456", null, "", null, null);
        }
    });
于 2012-11-18T20:39:57.240 回答