Android小工具+服务 [英] Android Widget+Service

查看:203
本文介绍了Android小工具+服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是服务,播放音乐音乐播放器,该服务中的所有广播接收器里面的服务,而不是外部定义。

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.

我完全新部件,所以我已经看到了一些tutorials.But他们没有帮助我很多
我完全新的未决soo.I感到如此困惑的意图,现在请帮我...
所有我想要做的只是触发使用widget的按钮,里面的服务广播...

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 ...

下面是复制粘贴code,我一直在试图理解

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

和这里是我的应用程序
:D

And here is my app :D

推荐答案

创建一个自定义的意图行动,并将其设置为的PendingIntent 小部件项(在你的情况按钮)

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

然后,更改舱单处理动作的PendingIntent

   <service android:name="com.example.app.services.CustomService" >
        <intent-filter>
            <action android:name="com.mediabook.app.ACTION_PLAY" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </service>

最后,点击播放按钮时,服务将获得动作并启动。检查动作 onStartCommand

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.

请参阅编译code这里 https://gist.github.com/androidbensin/4a9f044ac0b3110c049e

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

希望现在你的都不错。让我知道你的任何问题。

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

这篇关于Android小工具+服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆