Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – appWidget没有显示在启动器小部件列表在棒棒糖设备大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个 Android的应用程序,我已经在棒棒糖之前做了一个appwidget,由于某些原因,该小部件不会出现在棒棒糖.然而,它在前棒棒糖设备中出现.

这是我的代码

AndroidManifest.xml中

<receiver android:name=".widgets.NewsWidgetProvider"
        android:icon="@drawable/ic_launcher"
        android:label="@String/app_name" >>
        <intent-filter >
            <action
                android:name="android.appwidget.action.APPWIDGET_updatE" />
        </intent-filter>

        <Meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/news_info" />
</receiver>

news_info.xml

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

NewsWidgetProvider.java

public class NewsWidgetProvider extends appwidgetprovider {
private static final String NEXT_NEWS = "NEXT_NEWS";
private static final String PREVIoUS_NEWS = "PREVIoUS_NEWS";
private static final String GO_TO_NEWS_ACTIVITY = "GO_TO_NEWS_ACTIVITY";
private final String NEWS_LIST_KEY = "newsList";
NewsItem[] news;
int currentNews=0;
Bitmap imageBitmap;
private final String CURRENT_NEWS_KEY = "currentNews";

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

    startservice(context);

    loadItemFromSharedPreference(context);

    for (int widgetId : appWidgetIds) {
        setUpView(context,appWidgetManager,appWidgetIds,widgetId);
    }

}


@Override
public void onReceive(Context context,Intent intent) {
    super.onReceive(context,intent);
    loadItemFromSharedPreference(context);

    if (news != null) {
        if (GO_TO_NEWS_ACTIVITY.equals(intent.getAction())){
        openNewsActivity(context);
    }

        else if (NEXT_NEWs.equals(intent.getAction())) {
            if (currentNews == news.length - 1) {
                Toast.makeText(context,"reached last news",Toast.LENGTH_SHORT).show();
            }
        else if (news.length - 1 > currentNews) {
            currentNews++;
            SharedPreferencesmanager.putInteger(context,CURRENT_NEWS_KEY,currentNews);
            loadItemFromSharedPreference(context);
            setTheCurrentView(context);
        }
        }
    else if (PREVIoUS_NEWs.equals(intent.getAction())) {
                if (currentNews == 0) {
                    Toast.makeText(context,context.getString(R.String.last_item),Toast.LENGTH_SHORT).show();
                }
                else if (currentNews > 0 && news != null) {
                    currentNews--;
                    SharedPreferencesmanager.putInteger(context,currentNews);
                    loadItemFromSharedPreference(context);
                    setTheCurrentView(context);
                }
            }

    }
}

private void setUpView(Context context,int[] appWidgetIds,int widgetId) {
    final RemoteViews remoteViews = new RemoteViews(context.getPackagename(),R.layout.widget_news);

    remoteViews.setTextViewText(R.id.title,news==null?context.getString(R.String.wait_msg):news[currentNews].titlE);

    remoteViews.setTextViewText(R.id.brief,news==null?"":news[currentNews].brief);
    setPendingIntents(context,remoteViews);
    if (news!=null) {
        if (imageBitmap!=null) {
            AQuery aq = new AQuery(context);
            remoteViews.setImageViewBitmap(R.id.image,aq.getCachedImage(news[currentNews].imageUrl,100));
        }
        else {
            remoteViews.setImageViewresource(R.id.image,R.drawable.ic_launcher);
        }
    }
    else {
        remoteViews.setImageViewresource(R.id.image,R.drawable.ic_launcher);
    }
    appWidgetManager.updateAppWidget(widgetId,remoteViews);
}


private void setTheCurrentView(Context context) {
    RemoteViews remoteViews = new RemoteViews(context.getPackagename(),R.layout.widget_news);
    remoteViews.setTextViewText(R.id.title,news==null?context.getString(R.String.widget_problem_titlE):news[currentNews].titlE);
    remoteViews.setTextViewText(R.id.brief,news==null?context.getString(R.String.widget_problem_breif):news[currentNews].brief);
    remoteViews.setImageViewBitmap(R.id.image,imageBitmap);
    ComponentName thisWidget = new ComponentName( context,NewsWidgetProvider.class );
    AppWidgetManager.geTinstance(context).updateAppWidget( thisWidget,remoteViews );
}

private void openNewsActivity(Context context) {
    Intent o=new Intent(context,NewsActivity.class);
    o.putExtra(NewsItem.EXTRA_NEWS_ITEM,news[currentNews]);
    o.putExtra(NewsItem.EXTRA_NEWS_sourcE_IS_PUSH_OR_WIDGET,truE);
    o.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(o);
}

private void setPendingIntents(Context context,RemoteViews remoteViews) {
    remoteViews.setOnClickPendingIntent(R.id.next_news,getPendingSelfIntent(context,NEXT_NEWS,appWidgetIds));
    remoteViews.setOnClickPendingIntent(R.id.prevIoUs_news,PREVIoUS_NEWS,appWidgetIds));
    remoteViews.setOnClickPendingIntent(R.id.title,GO_TO_NEWS_ACTIVITY,appWidgetIds));
    remoteViews.setOnClickPendingIntent(R.id.brief,appWidgetIds));
    remoteViews.setOnClickPendingIntent(R.id.image,appWidgetIds));
}

protected PendingIntent getPendingSelfIntent(Context context,String action,int[] appWidgetIds) {
    Intent intent = new Intent(context,getClass());
    intent.setAction(action);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,appWidgetIds);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context,intent,0);
    return pendingIntent;
}
private void loadItemFromSharedPreference(Context context) {

    currentNews=SharedPreferencesmanager.getInteger(context,0);
    news=SharedPreferencesmanager.getObject(context,NEWS_LIST_KEY,NewsItem[].class);
    AQuery aq = new AQuery(context);
    imageBitmap= news == null ? BitmapFactory.decoderesource(context.getresources(),R.drawable.ic_launcher) : aq.getCachedImage(news[currentNews].imageUrl,200);
}

private void startservice(Context context) {
    Intent broadcasTintent = new Intent(context,NewsWidgetservice.class );
    //only start service if news is null
    if (SharedPreferencesmanager.getObject(context,NewsItem[].class)==null)
        context.startservice(broadcasTintent);
}

}

代码正在(kitkat,jelly …)工作,但它不能在棒棒糖工作

我错过了什么吗?

解决方法

我有同样的问题 – 我通过添加额外的参数android:widgetCategory =“home_screen”到appwidget-provider修复它:
<appwidget-provider xmlns:android="http://scheR_406_11845@as.android.com/apk/res/android"
    android:minWidth="294dp"
    android:minHeight="40dp"
    android:updatePeriodMillis="86400000"
    android:configure="com.example.activities.ConfigurationActivity"
    android:previewImage="@drawable/ic_launcher"
    android:initialLayout="@layout/widget_layout"
    android:widgetCategory="home_screen"
    android:resizeMode="none">
</appwidget-provider>

大佬总结

以上是大佬教程为你收集整理的android – appWidget没有显示在启动器小部件列表在棒棒糖设备全部内容,希望文章能够帮你解决android – appWidget没有显示在启动器小部件列表在棒棒糖设备所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。