在preSS事件Android小工具按钮 [英] android widget button on press event

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

问题描述

我试图创建窗口小部件按钮,直接通过主屏幕上运行的应用程序的活动。
小部件改变形象时,我根据my_button.xml文件preSS它。
但是当我点击它,我不能创建活动。

我需要知道如何才能叫我的主要活动里面的方法称之为上preSS事件(例如)。
我想AP preciate你的帮助

我附上相关的文件。

widget.xml

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=横向
    机器人:layout_height =WRAP_CONTENT的android:layout_width =WRAP_CONTENT>
  <按钮
        机器人:ID =@ + ID / button_one
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:填充=10dp
        机器人:背景=@绘制/ my_button/>
< / LinearLayout中>

my_button.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目机器人:可绘制=@绘制/ BTN1
          机器人:STATE_ pressed =真/>
    <项目机器人:可绘制=@绘制/ BTN2
          机器人:state_focused =真/>
    <项目机器人:可绘制=@绘制/ btn3/>
< /选择>

myWidgetProvider.java

 包com.callsift.activity;进口android.app.Notification;
进口android.app.NotificationManager;
进口android.app.PendingIntent;
进口android.appwidget.AppWidgetManager;
进口android.appwidget.AppWidgetProvider;
进口android.content.Context;
进口android.content.Intent;
进口android.util.Log;
进口android.widget.RemoteViews;
进口android.widget.Toast;公共类MyWidgetProvider扩展AppWidgetProvider {    公共静态字符串ACTION_WIDGET_CONFIGURE =ConfigureWidget;
    公共静态字符串ACTION_WIDGET_RECEIVER =ActionReceiverWidget;
        @覆盖
        公共无效的onUpdate(上下文的背景下,AppWidgetManager appWidgetManager,INT [] appWidgetIds){
            Toast.makeText(上下文中,的onUpdate,Toast.LENGTH_SHORT).show();            RemoteViews remoteViews =新的RemoteViews(context.getPackageName(),R.layout.widget);
            意图configIntent =新意图(背景下,CallSift.class);
            configIntent.setAction(ACTION_WIDGET_CONFIGURE);            意图激活=新意图(背景下,MyWidgetProvider.class);
            active.setAction(ACTION_WIDGET_RECEIVER);
            active.putExtra(味精,消息按钮1);            //的PendingIntent actionPendingIntent = PendingIntent.getBroadcast(上下文,0,活性,0);
            的PendingIntent configPendingIntent = PendingIntent.getActivity(上下文,0,configIntent,0);        // remoteViews.setOnClickPendingIntent(R.id.button_one,actionPendingIntent);
            remoteViews.setOnClickPendingIntent(R.id.button_one,configPendingIntent);
            appWidgetManager.updateAppWidget(appWidgetIds,remoteViews);
        }
        @覆盖        公共无效的onReceive(上下文的背景下,意图意图){            // V1.5修复不调用onDelete行动
            最后弦乐行动= intent.getAction();
            如果(AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(动作)){
                最终诠释appWidgetId = intent.getExtras()调用getInt(
                        AppWidgetManager.EXTRA_APPWIDGET_ID,
                        AppWidgetManager.INVALID_APPWIDGET_ID);
                如果(appWidgetId!= AppWidgetManager.INVALID_APPWIDGET_ID){
                    this.onDeleted(背景下,新的INT [] {appWidgetId});
                }
            }其他{
                //检查一下,如果我们的行动被称为
                如果(intent.getAction()。等于(ACTION_WIDGET_RECEIVER)){
                    Toast.makeText(上下文中,blblbl,Toast.LENGTH_SHORT).show();
                    弦乐味精=空;
                    尝试{
                        味精= intent.getStringExtra(味精);                    // Toast.makeText(背景下,测试,Toast.LENGTH_SHORT).show();
                    }赶上(NullPointerException异常五){
                        Log.e(错误,味精=空);
                    }
                    Toast.makeText(上下文中,TTTTT,Toast.LENGTH_SHORT).show();                    的PendingIntent contentIntent = PendingIntent.getActivity(上下文,0,意图,0);
                    NotificationManager notificationManager =(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
                    通知noty =新的通知(R.drawable.icon按钮1单击System.currentTimeMillis的());                    noty.setLatestEventInfo(背景下,通知,味精,contentIntent);
                    notificationManager.notify(1,noty);                }其他{
                    // 没做什么
                }                super.onReceive(背景下,意图);
            }
        }
    }

androidmainfast.xml - Relavent部分

 <! - 广播接收器将处理AppWidget更新 - >
<接收机器人:MyWidgetProviderNAME =机器人:标签=@字符串/ APP_NAME>
    &所述;意图滤光器>
        <作用机器人:名字=android.appwidget.action.APPWIDGET_UPDATE/>
        <! - 广播接收器,也将处理我们的自我创建的动作 - >
            <作用机器人:名字=com.callsift.activity.MyWidgetProvider.ACTION_WIDGET_RECEIVER/>
    &所述; /意图滤光器>
    <元数据机器人:名字=android.appwidget.provider机器人:资源=@ XML / button_widget_provider/>
< /接收器>


解决方案

您不能为了叫我的主要活动里面的方法事件(例如)对preSS一个应用程序窗口小部件呼叫。一个应用程序窗口小部件可以调用的PendingIntent ,这意味着它可以启动一个活动,启动服务,或发送一个广播 - 就是它了。它不能调用一些任意其他对象中的方法。

您能来最接近的是使用一个独特的意图 getActivity() 的PendingIntent (例如,自定义操作字符串),使用 setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP)意图来尝试将您的活动的现有实例向前,然后检查你的特殊意图在两个的onCreate() onNewIntent()您的活动,做任何特别的东西,你想要做的。

I'm trying to create a widget button to run an application activity directly through the home screen. the widget change the image when i press it according to the my_button.xml file. but i can't create an event when i click on it.

i need to know how to call "onPress" event (for example) in order to call a method inside my main activity. I would appreciate your help

i attached the relevant files.

widget.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_height="wrap_content" android:layout_width="wrap_content">


  <Button
        android:id="@+id/button_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:background="@drawable/my_button" />
</LinearLayout>

my_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/btn1"
          android:state_pressed="true" />
    <item android:drawable="@drawable/btn2"
          android:state_focused="true" />
    <item android:drawable="@drawable/btn3" />
</selector>

myWidgetProvider.java

package com.callsift.activity;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

public class MyWidgetProvider extends AppWidgetProvider {

    public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
    public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";




        @Override
        public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
            Toast.makeText(context, "onUpdate", Toast.LENGTH_SHORT).show();

            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
            Intent configIntent = new Intent(context, CallSift.class);
            configIntent.setAction(ACTION_WIDGET_CONFIGURE);

            Intent active = new Intent(context, MyWidgetProvider.class);
            active.setAction(ACTION_WIDGET_RECEIVER);
            active.putExtra("msg", "Message for Button 1");

            //PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
            PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);

        //  remoteViews.setOnClickPendingIntent(R.id.button_one, actionPendingIntent);
            remoteViews.setOnClickPendingIntent(R.id.button_one, configPendingIntent);


            appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
        }


        @Override

        public void onReceive(Context context, Intent intent) {

            // v1.5 fix that doesn't call onDelete Action
            final String action = intent.getAction();
            if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
                final int appWidgetId = intent.getExtras().getInt(
                        AppWidgetManager.EXTRA_APPWIDGET_ID,
                        AppWidgetManager.INVALID_APPWIDGET_ID);
                if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
                    this.onDeleted(context, new int[] { appWidgetId });
                }
            } else {
                // check, if our Action was called
                if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
                    Toast.makeText(context, "blblbl", Toast.LENGTH_SHORT).show();
                    String msg = "null";
                    try {
                        msg = intent.getStringExtra("msg");

                    //  Toast.makeText(context, "test", Toast.LENGTH_SHORT).show();


                    } catch (NullPointerException e) {
                        Log.e("Error", "msg = null");
                    }
                    Toast.makeText(context, "ttttt", Toast.LENGTH_SHORT).show();

                    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
                    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
                    Notification noty = new Notification(R.drawable.icon, "Button 1 clicked", System.currentTimeMillis());

                    noty.setLatestEventInfo(context, "Notice", msg, contentIntent);
                    notificationManager.notify(1, noty);

                } else {
                    // do nothing
                }

                super.onReceive(context, intent);
            }
        }
    }

androidmainfast.xml - Relavent part

        <!-- Broadcast Receiver that will process AppWidget updates -->
<receiver android:name=".MyWidgetProvider" android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        <!-- Broadcast Receiver that will also process our self created action -->
            <action android:name="com.callsift.activity.MyWidgetProvider.ACTION_WIDGET_RECEIVER"/>
    </intent-filter>
    <meta-data android:name="android.appwidget.provider" android:resource="@xml/button_widget_provider" />
</receiver>

解决方案

You cannot have an app widget "call "onPress" event (for example) in order to call a method inside my main activity". An app widget can invoke a PendingIntent, which means it can start an activity, start a service, or send a broadcast -- that is it. It cannot call a method within some arbitrary other object.

The closest you will be able to come is to use a distinctive Intent in the getActivity() PendingIntent (e.g., a custom action string), use setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP) on the Intent to try to bring an existing instance of your activity forward, then check for your special Intent in both onCreate() and onNewIntent() of your activity and do whatever special thing you want done.

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

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