gpt4 book ai didi

java - 新闻事件上的android小部件按钮

转载 作者:搜寻专家 更新时间:2023-11-01 07:40:22 25 4
gpt4 key购买 nike

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

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

我附上了相关文件。

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>

最佳答案

您不能让应用小部件“调用“onPress”事件(例如)以调用我的主要 Activity 中的方法”。应用程序小部件可以调用 PendingIntent ,这意味着它可以启动 Activity 、启动服务或发送广播——仅此而已。它不能调用任意其他对象中的方法。

最接近的是使用独特的 IntentgetActivity() PendingIntent (例如,自定义操作字符串),使用 setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP)Intent 上尝试将您的 Activity 的现有实例向前推进,然后检查您的特殊 Intent在两个onCreate()onNewIntent()你的 Activity ,做任何你想做的特别的事情。

关于java - 新闻事件上的android小部件按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4722517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com