gpt4 book ai didi

Android AppwidgetProvider 从未调用过

转载 作者:行者123 更新时间:2023-11-29 02:02:05 24 4
gpt4 key购买 nike

我正在关注这个 http://www.vogella.com/articles/AndroidWidgets/article.html教程。

这是我的 Widetprovider:

import java.util.Random;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;

import com.asco.countdown.R;

public class MyWidgetProvider extends AppWidgetProvider {

private static final String ACTION_CLICK = "ACTION_CLICK";

@Override
public void onEnabled(Context context) {
Log.d("WID", "enabled");
}



@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {

Log.d("WID", "update");

// Get all ids
ComponentName thisWidget = new ComponentName(context,
MyWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
// Create some random data
int number = (new Random().nextInt(100));

RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget);
Log.w("WidgetExample", String.valueOf(number));
// Set the text
remoteViews.setTextViewText(R.id.update, String.valueOf(number));

// Register an onClickListener
Intent intent = new Intent(context, MyWidgetProvider.class);

intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
}

这是我的 widgetinfo.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget"
android:minHeight="72dp"
android:minWidth="300dp"
android:updatePeriodMillis="5000000" >

</appwidget-provider>

这是我的 list :

<?xml version="1.0" encoding="utf-8"?>

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<receiver android:name="MyWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="android.appwidget.action.APPWIDGET_ENABLED" />
<action android:name="android.appwidget.action.APPWIDGET_DELETED" />
<action android:name="android.appwidget.action.APPWIDGET_DISABLED" />
</intent-filter>

<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widgetinfo" />
</receiver>
</application>

显示了小部件,但既没有调用 onEnabled() 也没有调用 onUpdate() 方法,onClickListener 也不起作用。我试过在 list 中提供 AppWidgetProvider 的完整路径,将 AppWidgetProvider 放在另一个包中(现在它在默认包中)等等,但没有任何反应,现在我没有想法了。有人可以帮帮我吗?

最佳答案

尝试在maifest中给出全名

<receiver android:name="MyWidgetProvider" >

<receiver android:name="com.asco.countdown.MyWidgetProvider" > 

或者什么全名

关于Android AppwidgetProvider 从未调用过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12109287/

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