-1

I am using broadcast receiver class to read the messages automatically

But Iam facing some problems,

    1. I am using indent to pass the data to another activity, I do not know is this the proper approach? If any better way please let me know.
    1. My app automatically read the OTP from the message that was okay, but it reads every message whenever a new message arives (like customer care message and any) I want read the message only the app in running state.

Here my code,

public class IncomingSms extends BroadcastReceiver {

final SmsManager sms = SmsManager.getDefault();
String message;

@Override
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;
                message = currentMessage.getDisplayMessageBody();

                Intent a = new Intent(context, OtpConformation.class);
                a.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                a.putExtra("KEY_1", message);
                context.startActivity(a);

            }
        }

    } catch (Exception e) {
        Log.e("SmsReceiver", "Exception smsReceiver" +e);

    }
}

}`

In Another activity `

    Intent intent = getIntent();
    message = intent.getStringExtra("KEY_1");
    OtpEdt = (EditText)findViewById(R.id.Otp_editText);
    OtpEdt.setText(message);`

In My manifest

<receiver android:name=".IncomingSms"
        android:permission="android.permission.BROADCAST_SMS">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
Narandhran Thangavel
  • 1,392
  • 1
  • 12
  • 20
Vijay
  • 227
  • 3
  • 18

2 Answers2

0

Question:1 I am using intent to pass the data to another activity,but don't need intent, any other ideas for pass the data to another activity?

Ans: You may want to consider the SharedPreference objects. It has a simple API and is accessible across an application's activities. Here is an example - https://developer.android.com/guide/topics/data/data-storage.html#pref

refer official documentation here - https://developer.android.com/reference/android/content/SharedPreferences.html

Question 2:My app automatically read the message while app was closed time? i want read the message only app is running time

Answer : You can check app state by ActivityManager. Please refer following method.

public static boolean isAppRunning(Context context)
    {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfos = activityManager.getRunningAppProcesses();


        for (ActivityManager.RunningAppProcessInfo processInfo : runningAppProcessInfos) {

            if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                for (String activeProcess : processInfo.pkgList)
                {
                    Log.i("GCM", activeProcess + ":" + processInfo.importance);

                    if (activeProcess.equals(context.getPackageName())) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
Sangram Haladkar
  • 707
  • 9
  • 22
0
Community
  • 1
  • 1
Narandhran Thangavel
  • 1,392
  • 1
  • 12
  • 20