0

in Gosms application, we want to see new received sms message in phone desktop and i want to have this ability in own appication. i'm try simple use and show AlertDialog in onStop() like with this:

protected void onStop() {
        builder.setMessage("Test message")
                .setCancelable(true)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();
        super.onStop();
        Log.e(" onStop","");
    }

but this could not work correctly. after that i'm try to show this dialog by service. all ways dont work correctly for me. please help me ti have this ability

ScreenShot: enter image description here

  • You will have to open an activity(looks like a dialog) – Nadeem Iqbal Oct 28 '14 at 04:51
  • @NadeemIqbal i cant find any document about this. can you help me? how to open activity in `onStop`? –  Oct 28 '14 at 04:53
  • First of all, Correct me if I am wrong. 1) You want to Show a `dialog when there is a new msg received.???` 2) Why in `OnStop()`??? – Nadeem Iqbal Oct 28 '14 at 05:22
  • @NadeemIqbal i want to show new sms receive when application is in `onStop()` or user press home button on phone. like with gosms.i'm adding screen shot on post –  Oct 28 '14 at 05:39
  • you mean you want to show dialog only when the application is not currently running. – SweetWisher ツ Oct 28 '14 at 05:45
  • @SweetWisher yes that right –  Oct 28 '14 at 05:54
  • [Check this one to detect whether ur app is in foreground or not](http://stackoverflow.com/a/5862048/2591002) and if it's not, then show a dialog from a service which listen to msg arrival. – SweetWisher ツ Oct 28 '14 at 05:56

1 Answers1

1

You need to detect whether your app is currently in background or foreground.

Approach :

  1. Add a BroadcastReceiver to listen the incoming message
  2. Check whether your app is in foreground or not
  3. If it is in background, show the dialog box

Code Snippet :

Implement custom Application class which changes the application status

public class MyApplication extends Application {

  public static boolean isActivityVisible() {
    return activityVisible;
  }  

  public static void activityResumed() {
    activityVisible = true;
  }

  public static void activityPaused() {
    activityVisible = false;
  }

  private static boolean activityVisible;
}

Add onPause and onResume to every Activity in the project to change the application status :

@Override
protected void onResume() {
  super.onResume();
  MyApplication.activityResumed();
}

@Override
protected void onPause() {
  super.onPause();
  MyApplication.activityPaused();
}

Add the permission and register the receiver in manifest :

<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS"></uses-permission>
     <receiver android:name=".SMSBroadcastReceiver">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
            </intent-filter>
        </receiver>

SMSBroadcastReceiver :

public class SMSBroadcastReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Bundle bundle = intent.getExtras();
    SmsMessage[] msgs = null;
    String str = "no message received";
    if(bundle != null){

        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]);
            str += "SMS from Phone No: " +msgs[i].getOriginatingAddress();
            str +="\n"+"Message is: ";
            str += msgs[i].getMessageBody().toString();
            str +="\n";
        }

        Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); //contact number and the message

     // Now as soon as you get the message check whether your application is running or not and show the dialog

        if(!MyAppliction.isActivityVisible())
       {
               // code to show a dialog box
       }
    }
}}
Community
  • 1
  • 1
SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74
  • thankyou very mush sir. for this action i get sms with json into application. how change manifest to have this ability? my mean is this line `` –  Oct 28 '14 at 06:41
  • I didn't get you...Sorry.. Try ma code and you will get the message and the contact number at toast – SweetWisher ツ Oct 28 '14 at 06:53