新闻事件的android小部件按钮

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

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

我附上了相关文件。

widget.xml

  

my_button.xml

       

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

           

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

你最接近的是在getActivity() PendingIntent使用一个独特的Intent (例如,一个自定义动作字符串),在Intent上使用setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP)来尝试引入现有实例你的活动向前,然后在你的活动的onCreate()onNewIntent()中检查你的特殊Intent ,做你想做的任何特别的事情。

您可以通过在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); 

}