在我的简单示例应用中,我有2项活动:
同样在我的应用程序中,我有 NewAppWidget.java 类扩展 AppWidgetProvider
我想要完成的是:
当我将主页小部件添加到屏幕时,我想首先打开活动NewAppWidgetConfigureActivity.java。到目前为止一切正常,它工作得很好。
当我将主页小部件添加到屏幕时,我的小部件有2个按钮和一个TextView。 我想要的是这个:
然而,它不起作用。点击按钮没有任何反应。我尝试从其他问题实施解决方案,但我没有工作。
以下是代码:
清单文件代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.petar.mywidgetwitbuttons">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".NewAppWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/new_app_widget_info" />
</receiver>
<activity android:name=".NewAppWidgetConfigureActivity">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
</application>
这是 new_ap_widget_info.xml 的代码,其中我添加了我的小部件的所有必要信息,包括我添加时自动打开活动 NewAppWidgetConfigureActivity.java 的代码行窗口小部件。
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:configure="com.example.petar.mywidgetwitbuttons.NewAppWidgetConfigureActivity"
android:initialKeyguardLayout="@layout/new_app_widget"
android:initialLayout="@layout/new_app_widget"
android:minHeight="110dp"
android:minWidth="180dp"
android:previewImage="@drawable/example_appwidget_preview"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="86400000"
android:widgetCategory="home_screen" />
这是扩展 AppWidgetProvider
的 NewAppWidget.java 类的代码package com.example.petar.mywidgetwitbuttons;
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;
/**
* Implementation of App Widget functionality.
* App Widget Configuration implemented in {@link NewAppWidgetConfigureActivity NewAppWidgetConfigureActivity}
*/
public class NewAppWidget extends AppWidgetProvider {
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
CharSequence widgetText = NewAppWidgetConfigureActivity.loadTitlePref(context, appWidgetId);
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
views.setTextViewText(R.id.appwidget_text, widgetText);
Log.d("widget", "onUpdatePrvo: Widget testiramo");
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
Intent intent = new Intent(context, MainActivity.class);
Intent intent2 = new Intent(context, NewAppWidgetConfigureActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 0, intent2, 0);
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
views.setOnClickPendingIntent(R.id.buttonCLICK, pendingIntent);
views.setOnClickPendingIntent(R.id.buttonCLICK2, pendingIntent2);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// When the user deletes the widget, delete the preference associated with it.
for (int appWidgetId : appWidgetIds) {
NewAppWidgetConfigureActivity.deleteTitlePref(context, appWidgetId);
}
}
@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}
@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
}
另外,当我从提供商信息中删除这行代码时,我可以在第一个按钮点击时打开 MainActivity.java ,但仍然无法打开 NewAppWidgetConfigureActivity.java 点击第二个按钮。 机器人:配置= “com.example.petar.mywidgetwitbuttons.NewAppWidgetConfigureActivity”
这里是完整的提供商信息代码,没有 android:configure =“”
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:configure="com.example.petar.mywidgetwitbuttons.NewAppWidgetConfigureActivity"
android:initialKeyguardLayout="@layout/new_app_widget"
android:initialLayout="@layout/new_app_widget"
android:minHeight="110dp"
android:minWidth="180dp"
android:previewImage="@drawable/example_appwidget_preview"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="86400000"
android:widgetCategory="home_screen" />
答案 0 :(得分:0)
这些简单的步骤可以帮助您解决问题。
App小部件允许某些UI控件而不是一切。因此,检查允许的内容和不允许的内容。 https://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout
不要扩展或创建自定义UI控件类。 Ex; - 类CustomButton扩展Button
我确实看到你的Intents没有额外的参数。这可能会在内部混淆PendingIntent比较逻辑。因此,为intent添加自定义Action或一些额外参数。 EX; -
意图a =新意图(&#34; MainActivity&#34;); 意图b =新意图(&#34; ConfigureActivity&#34;);
答案 1 :(得分:0)
这对我有用。
public class widget_wordss extends AppWidgetProvider {
private static final String ACTION_SIMPLEAPPWIDGET = "ACTION_BROADCASTWIDGETSAMPLE";
private static final String ACTION_SIMPLEAPPWIDGET1 = "ACTION_BROADCASTWIDGETSAMPLE1";
public boolean widg = false;
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int
appWidgetId) {
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget_main_words);
Intent intent = new Intent(context, widget_wordss.class);
Intent intent1 = new Intent(context, widget_wordss.class);
intent.setAction(ACTION_SIMPLEAPPWIDGET);
intent1.setAction(ACTION_SIMPLEAPPWIDGET1);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 0, intent1,
PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.btnOpen, pendingIntent);
views.setOnClickPendingIntent(R.id.btnayar, pendingIntent1);
appWidgetManager.updateAppWidget(appWidgetId, views);