开发者

android widget button on press event

开发者 https://www.devze.com 2023-02-05 10:28 出处:网络
I\'m trying to create a widget button to run an application activity directly through the home screen.

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"
          and开发者_开发问答roid: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.


You can launch the new activity like this by changing your code on OnReceive Like this.

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

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

}

0

精彩评论

暂无评论...
验证码 换一张
取 消