android service更新widget,Android Widget+Service

news/2024/11/9 0:42:54/

问题

I have a music player which plays music using a service,all the broadcast receivers within that service are defined inside the service and not externally.

I am totally new to widgets so i had been seeing a few tutorials.But they didn't help me much

I am totally new to pending intents soo.I am so confused right now please help me out...

All i want to do is just trigger the broadcast inside the service using the button of the widget ...

Here is the copy pasted code which i had been trying to understand

RemoteViews controlButtons = new RemoteViews(context.getPackageName(),

R.layout.widget);

Intent playIntent = new Intent(context, Music_service.class);

PendingIntent playPendingIntent = PendingIntent.getService(

context, REQUEST_CODE, playIntent, INTENT_FLAGS);

controlButtons.setOnClickPendingIntent(

R.id.bPlay, playPendingIntent);

appWidgetManager.updateAppWidget(appWidgetIds, controlButtons);

And here is my app

:D

f0db5dafdd61373f33ceabe6380bec47.png

回答1:

Create a custom Intent Action and set it as PendingIntent to the widget item ( button in your case)

Intent intent = new Intent("com.example.app.ACTION_PLAY");

PendingIntent pendingIntent = PendingIntent.getService(context.getApplicationContext(), 100,

intent, PendingIntent.FLAG_UPDATE_CURRENT);

RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_layout);

remoteViews.setOnClickPendingIntent(R.id.bPlay, pendingIntent);

Then, change your manifest to handle the Action passed in PendingIntent

Finally , when the play button is clicked , the Service will receive the Action and started. Check for the Action in onStartCommand

@Override

public int onStartCommand(Intent intent, int flag, int startId) {

if(intent.getAction().equalsIgnoreCase("com.example.app.ACTION_PLAY")){

// do your stuff here

}

You can set similar custom Actions for all the required views in the Widget.

See the compile code here https://gist.github.com/androidbensin/4a9f044ac0b3110c049e

Hope your are good now. Let me know if any issues

来源:https://stackoverflow.com/questions/23553816/android-widgetservice


http://www.ppmy.cn/news/291291.html

相关文章

Linux内核配置蓝牙驱动,android bluetooth蓝牙移植,androidbluetooth

android bluetooth蓝牙移植,androidbluetooth 前段时间移植神念系统需要使用到bluetooth功能,现将移植过程中碰到的问题简要列一下,备忘而已。 1、内核增加配置 make menuconfig -> Networking support -> Bluetooth subsystem support --> 进行选择BT选项,至少选…

Linux恐慌信息详解,调试linux内核恐慌

我是内核调试的新手.Below是最后一个内核消息.根据我的理解,它试图访问空指针.但我不明白为什么它会关闭杯子0. Unable to handle kernel paging request at virtual address 40025694 [37655.473976] pgd d7138000 [37655.474007] [40025694] *pgd9ec1a831, *ptebf2e659d, *p…

flink状态和容错(三)

目录 state容错语义at most onceat least onceexactly-once 容错续跑算子容错续跑task异常作业逻辑不变CP/SP策略bugfix升级续跑SP策略 source容错续跑sink 容错续跑 state flink 支持有状态的流,存储历史的状态信息。 状态 状态分类 keystate keyBy/groupBy/Partit…

anyview+4.0java版_Anyview v4.0.12[带证书]

软件简介 Anyview可以阅读任意TXT文件,包括UNICODE、UTF-8、GB2312、HTML文件、PDB文件、UMD文件。 对于TXT文件,测试过8M的文件,处理速度可以接受。 Anyview支持滚屏,可以自己调整滚屏的速度;在阅读时,可以切换到全屏方式。 每5分钟,Anyview会为您保存一次阅读历史 有多…

老java手机有什么什么用,80后熟悉的这些手机功能,用过3种以上说明你已经老了...

原标题:80后熟悉的这些手机功能,用过3种以上说明你已经老了 又是一年元旦时,每到这时怀旧的心情便一发不可收拾。朋友圈等晒18岁照片成了热潮,可怜亓纪18岁的时候还没有手机。只有怀念一下那些年少轻狂时代用过的手机功能。 红外接…

获取指定文件夹下所有的同种类型文档的名字(Python)

今天教大家如何在一个非常混乱文件夹下用Python编写一个程序来寻找所有自己所需类型的文件(这次举例选择json文件,如果有寻找其他类型文件需要可下面程序json改为自己所需的文件名字),并且输出所有所需文件的名字。 首先我们需要…

第七届湖湘杯网络安全大赛 - 初赛writeup

Web easywill 解题思路 变量覆盖 http://eci-2zej1goyn9jh8hty6ton.cloudeci1.ichunqiu.com/?namecfile&value/etc/passwd P神博客最近的文章利用pearcmd:https://tttang.com/archive/1312/ Pentest in Autumn 解题思路http://eci-2ze40jm526y24nv2lkl3.cloude…

Cookie(Secure和HttpOnly属性) JSESSIONID 刷新

目录 一:Cookie(Secure和HttpOnly属性) 二:JSESSIONID是什么 三: JSESSIONID 刷新 一:Cookie(Secure和HttpOnly属性) 基于安全的考虑,需要给cookie加上Secure和HttpOn…