如何在小部件按钮点击时启动服务?

3

我能在小部件按钮点击时启动Activity类,但是尝试同样的方式启动服务时却无法工作,请告诉我如何在这里调用start service。

谢谢

2个回答

0
使用这行代码。
startService(new Intent(this, YOUR_SERVICE_CLASS_NAME.class)); 

-1

试试这段代码吧

Intent i = new Intent(this, MyWidget.class);
PendingIntent pi = PendingIntent.getBroadcast(context,0, i,0);
RemoteViews updateView = new RemoteViews(context.getPackageName(),R.layout.n);
updateViews.setOnClickPendingIntent(R.id.update_button,pi);

这是一个简单的活动按钮,但请告诉我如何使用小部件按钮来实现它。 - CoDe
1
@Shubh,你找到解决方案了吗?我遇到了类似的问题,我的小部件按钮事件丢失了,因为服务没有启动。 - JRC
@JRC 这是很久以前的事了,但我记不太清楚了,你可以查看这些链接:button widget click not workingStart Stop Service from Widget,或者从this中获取信息。 - CoDe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接