新闻事件的android小部件按钮

时间:2011-01-18 09:35:17

标签: java android android-widget

我正在尝试创建一个小部件按钮,以直接通过主屏幕运行应用程序活动。 根据my_button.xml文件按下它时,窗口小部件会更改图像。 但是当我点击它时我无法创建一个事件。

我需要知道如何调用“onPress”事件(例如)以调用我的主要活动中的方法。 非常感谢您的帮助

我附上了相关文件。

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 - 相关部分

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

2 个答案:

答案 0 :(得分:4)

你不能让app小部件“调用”onPress“event(例如),以便调用我的主要活动中的方法”。应用小部件可以调用PendingIntent,这意味着它可以启动活动,启动服务或发送广播 - 就是这样。它无法在某个任意其他对象中调用方法。

您最接近的是使用Intent getActivity()中的独特PendingIntent(例如,自定义操作字符串),在setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP)上使用Intent Intent尝试将您活动的现有实例转发,然后检查您活动的onCreate()onNewIntent()中的特殊{{1}},并执行您想要的任何特殊操作完成。

答案 1 :(得分:1)

您可以通过更改OnReceive上的代码来启动这样的新活动。

  if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {

      Intent newIntent = new Intent("YOUR_INTENT_ACTION");   
       newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       context.startActivity(newIntent);

}