简体   繁体   中英

transfer / send string variables from one java class to other in android

the code below gives me the phone number and message from my inbox.... I need to transfer / send both phone number and message to other java class in android (not the activity but class) so to save in db .

it might be a basic question but i am unable to solve it if anyone could help...it would be great

Main activity:

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.widget.Toast;

import static android.telephony.SmsMessage.createFromPdu;

public class IncomingSms extends BroadcastReceiver {


    String p,m;

    final SmsManager sms = SmsManager.getDefault();


    public void onReceive(Context context, Intent intent) {

        Bundle extras = intent.getExtras();

        if (extras != null) {
            Object[] smsExtra = (Object[]) extras.get( "pdus" );
            ContentResolver contentResolver = context.getContentResolver();

            for (int i = 0; i < smsExtra.length; ++i) {

                SmsMessage sms = createFromPdu( (byte[]) smsExtra[i] );

                String phoneNumber = sms.getDisplayOriginatingAddress();
                String message = sms.getDisplayMessageBody();

                try {
                    if (phoneNumber.contains( "+92xxxxxxxxxx" )) {   //add phone number
                        int duration = Toast.LENGTH_LONG;
                        Toast toast = Toast.makeText( context,
                                "senderNum: " + phoneNumber + ", message: " + message, duration );
                        toast.show();

                        p=phoneNumber;
                        m=message;

                    }
                } catch (Exception e) {
                }

            }
        }
    }
}

If you have your activity named ReceiveText , then in your BroadcastReceiver , you should do the following:

Intent i = new Intent(context, ReceiveText.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("message", message.getMessageBody());
context.startActivity(i);

Then, in your activity , you will need to getExtra as so:

Intent intent = getIntent();
String message = intent.getStringExtra("message");

Let's say you have a class that handles all DB related queries named: DBQueries If you want to save your message and number then your DBQueries class should have a method as follows:

public class DBQueries {
    public void save(String number, String message) {
        //Your db insertion code here
    }
}

Now in your above code, call this method as follows:

public class IncomingSms extends BroadcastReceiver {

    String p,m;

    final SmsManager sms = SmsManager.getDefault();

    public void onReceive(Context context, Intent intent) {

        Bundle extras = intent.getExtras();

        if (extras != null) {
            Object[] smsExtra = (Object[]) extras.get( "pdus" );
            ContentResolver contentResolver = context.getContentResolver();

            for (int i = 0; i < smsExtra.length; ++i) {

                SmsMessage sms = createFromPdu( (byte[]) smsExtra[i] );

                String phoneNumber = sms.getDisplayOriginatingAddress();
                String message = sms.getDisplayMessageBody();

                try {
                    if (phoneNumber.contains( "+92xxxxxxxxxx" )) {   //add phone number
                        int duration = Toast.LENGTH_LONG;
                        DBQueries dbQueries = new DBQueries();
                        dbQueries.save(phoneNumber, message);
                        Toast toast = Toast.makeText( context,
                                "senderNum: " + phoneNumber + ", message: " + message, duration );
                        toast.show();

                        p=phoneNumber;
                        m=message;

                    }
                } catch (Exception e) {
                }

            }
        }
    }
}

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