通过Intent向service传递数据

什么是intent的action?

    /**
     * Create an intent with a given action.  All other fields (data, type,
     * class) are null.  Note that the action <em>must</em> be in a
     * namespace because Intents are used globally in the system -- for
     * example the system VIEW action is android.intent.action.VIEW; an
     * application's custom action would be something like
     * com.google.app.myapp.CUSTOM_ACTION.
     *
     * @param action The Intent action, such as ACTION_VIEW.
     */
    public  Intent(String  action) {
        setAction(action);
    }
 
 client 侧调用service, 进程间的对象传输使用Bundle 


  Intent intent = new Intent("action_upload_subsys_report");

    ComponentName component = new ComponentName("com.xxx", "com.xxx.service.yyyService");
    intent.setComponent(component);
    Bundle bundle = new Bundle();
    bundle.putString("subsys", subsys);
    bundle.putString("keyPattern", keyPattern);
    bundle.putString("type", type);
    intent.putExtras(bundle);
    startServiceAsUser(intent, UserHandle.CURRENT);
 
server侧:
 
onStartCommand {
	
	if (intent.getAction().equalsIgnoreCase(ACTION_UPLOAD_SUBSYS_REPORT)) {
		if (intent.getExtra() != null) {
			sendMessages(HANDLE_REPORT, intent.getExtras());
		}
	}
}

handleMessage(Message msg) {

case HANDLE_REPORT:

Bundle bundle = (Bundle) msg.obj;

String time = bundle.getString("keyPattern");

String type = bundle.getString("type");

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值