开发者

Transfer data between BroadCast and Activity

开发者 https://www.devze.com 2023-02-28 06:21 出处:网络
how can i transfer data from BroadcastReceiver to activity? I tried it with intent but it\'s returning null.

how can i transfer data from BroadcastReceiver to activity? I tried it with intent but it's returning null.

MyBroadcastReceiver class:

public class MyBroadcastReceiver extends BroadcastReceiver {

    private static final String TAG = "MyBroadcastReceiver";
    @Override
       public void onReceive(Context context, Intent intent) {

        ......................................
            ......................................

        String phoneNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
            Intent i = new Intent(context, CalendarCall.class);
        i.putExtra("number", phoneNumber);
       开发者_运维问答     (startActivity(i)-> i can't start Activity under BroadcastReceiver,what is another way to do that? )
        Log.d(TAG, phoneNumber);
                }
         }
}

}

Main Activity:

Intent intent = getIntent();
        String number = intent.getStringExtra("number");
        Log.d(TAG, "Number: " + number);


MyBroadcastReceiver class:

public class MyBroadcastReceiver extends BroadcastReceiver {

    private static final String TAG = "MyBroadcastReceiver";
    @Override
       public void onReceive(Context context, Intent intent) {

        ......................................
            ......................................

        String phoneNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
            Intent i = new Intent(context, CalendarCall.class);
        i.putExtra("number", phoneNumber);
        context.startActivity(i);
        Log.d(TAG, phoneNumber);
                }
         }
}

}

try this code

0

精彩评论

暂无评论...
验证码 换一张
取 消