简体   繁体   中英

My android program not replaying

Well i have made this program it receive sms and auto replay but it recive but do not replay Here is my 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{

        }    

    }
}
}

My SMS SENDING CLASS

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



}

My menifest.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>

Can any one tell me the problem it is showing toast when sms recive but not replaying

Never do Main_Activity obj = new Main_Activity(); Activity should only EVER be handled by the OS, create a utility class.

The onRecieve is not guaranteed to be on your Application UI thread try:

Try using a handler with your Looper explicitly set:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM