在BroadCast和Activity之间传输数据

时间:2011-04-22 12:31:03

标签: android

如何将数据从BroadcastReceiver传输到活动? 我故意尝试了它,但它返回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);
                }
         }
}

}

主要活动:

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

1 个答案:

答案 0 :(得分:1)

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);
                }
         }
}

}

试用此代码