문제

i am new to BroadcastReceiver concepts.

when the app receives the message it shows the message using toast.makeText. but the app didnt open. how can i open the app please help.

This is my Activiy Class

     public class BroadcastNewSms extends Activity {
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.androidexample_broadcast_newsms);}}

This is my IncomingSms seperate class when receiving it shows only the message but the app didnt open. how can i do that.

      public class IncomingSms extends BroadcastReceiver {
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++) {
                int duration = Toast.LENGTH_LONG;
                    SmsManager sms = SmsManager.getDefault();
                     sms.sendTextMessage("7373457769", null, message, null, null);
                Toast toast = Toast.makeText(context, "senderNum: "+ senderNum + ", message: " + message, duration);
                toast.show();
            } // end for loop
          } // bundle is null

    } catch (Exception e) {
        Log.e("SmsReceiver", "Exception smsReceiver" +e);}}}
도움이 되었습니까?

해결책

try this:

public class IncomingSms extends BroadcastReceiver {
    public static final String NUMBER = "NUMBER";
    public static final String MESSAGE = "MESSAGE";

    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++) {
                    int duration = Toast.LENGTH_LONG;
                    SmsManager sms = SmsManager.getDefault();
                    sms.sendTextMessage("7373457769", null, message, null, null);
                    Toast toast = Toast.makeText(context, "senderNum: "+ senderNum + ", message: " + message, duration);
                    toast.show();

                    // !!!
                    Intent intent = new Intent(context, BroadcastNewSms.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra(NUMBER, senderNum);
                    intent.putExtra(MESSAGE, message);
                    context.startActivity(intent);

                } // end for loop
            } // bundle is null

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


public class BroadcastNewSms extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.androidexample_broadcast_newsms);

        // !!!
        Intent intent = getIntent();
        String number = intent.getStringExtra(IncomingSms.NUMBER);
        String message = intent.getStringExtra(IncomingSms.MESSAGE);
    }
}

다른 팁

You can open your activity adding this instead of your Toast

Intent i = new Intent();
i.setClassName(context.getApplicationContext(), BroadcastNewSms.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//add some Extras to send it to your activity
context.startActivity(i);

the above code, will open an activity when message is received, however I prefer if you showed a notification and on notification click you open the activity.

Please give me a feedback if it worked. good luck

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top