Android 通过Log完整打印Intent 内容

		
        Intent intent = getIntent();
        // intent信息
        Log.e(TAG,intent.toString());
        
		Bundle bundle = intent.getExtras();
		// extras intent附带信息
        for (String key: bundle.keySet())
        {
            Log.e(TAG, "Key=" + key + ", content=" +bundle.get(key));//注意这里:尽量使用get,可以打印所有类型。
        }
       
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来给您讲一下如何在 Android Activity 中发送不同的广播内容,并在 Service 中接收广播并打印 log。 首先在 Activity 中发送不同的广播内容,可以通过 Intent 中的不同 Extra 数据来实现。示例代码如下: ``` // 发送第一条广播 Intent intent1 = new Intent(); intent1.setAction("com.example.broadcast.MY_BROADCAST"); intent1.putExtra("data", "Hello, Service! This is the first broadcast."); sendBroadcast(intent1); // 发送第二条广播 Intent intent2 = new Intent(); intent2.setAction("com.example.broadcast.MY_BROADCAST"); intent2.putExtra("data", "Hello, Service! This is the second broadcast."); sendBroadcast(intent2); ``` 这里我们发送了两条广播,它们的 Action 都是 "com.example.broadcast.MY_BROADCAST",但 Extra 数据不同。 接下来在 Service 中接收广播,同样可以通过注册 BroadcastReceiver 来实现。示例代码如下: ``` public class MyService extends Service { private BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if ("com.example.broadcast.MY_BROADCAST".equals(action)) { String data = intent.getStringExtra("data"); Log.d("MyService", "Received broadcast: " + data); } } }; @Override public void onCreate() { super.onCreate(); IntentFilter filter = new IntentFilter(); filter.addAction("com.example.broadcast.MY_BROADCAST"); registerReceiver(receiver, filter); } @Override public void onDestroy() { super.onDestroy(); unregisterReceiver(receiver); } @Nullable @Override public IBinder onBind(Intent intent) { return null; } } ``` 这里我们的 BroadcastReceiver 代码与上一个回答中的代码相同。当接收到广播时,我们从 Intent 中获取传递的数据,并打印 log。 因为在发送广播时 Extra 数据不同,所以在 Service 中打印log 也会不同。这样就实现了在 Activity 中发送不同的广播内容,并在 Service 中接收广播并打印 log 的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值