gpt4 book ai didi

x 时间后 Android 小部件更新

转载 作者:搜寻专家 更新时间:2023-11-01 09:11:06 26 4
gpt4 key购买 nike

我刚开始学习 Android,我正在尝试编写一个每秒钟更新一次计数器的小部件,但在某处它丢失了一些东西。我在屏幕上看到 1。我正在使用 Android 4 模拟器。

package com.ibluekey;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import com.ibluekey.R;

import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

public class WidgetActivity extends AppWidgetProvider {
public static WidgetActivity Widgetke = null;
public static Context context;
public static AppWidgetManager appWidgetManager;
public static int appWidgetIds[];
private Handler mHandler = new Handler();
private static int count=0;
@Override
public void onUpdate( Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds ) {
if (null == context) context = WidgetActivity.context;
if (null == appWidgetManager) appWidgetManager = WidgetActivity.appWidgetManager;
if (null == appWidgetIds) appWidgetIds = WidgetActivity.appWidgetIds;

WidgetActivity.Widgetke = this;
WidgetActivity.context = context;
WidgetActivity.appWidgetManager = appWidgetManager;
WidgetActivity.appWidgetIds = appWidgetIds;

Log.d("PXR", "onUpdate");

final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];

updateAppWidget(context,appWidgetManager, appWidgetId);
}

}


static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {

count++;

/*Intent intent = new Intent(context, UpdateService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);

RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
remoteViews.setOnClickPendingIntent(R.id.mainlayout, pendingIntent);

remoteViews.setTextViewText(R.id.test_out, ""+count);

// Tell the widget manager
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);*/

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setTextViewText(R.id.test_out, count+"");
appWidgetManager.updateAppWidget(appWidgetId, views);
}



public static class UpdateService extends Service {
@Override
public void onStart(Intent intent, int startId) {
//WidgetActivity.Widgetke.onUpdate(context, appWidgetManager, appWidgetIds);
//Toast.makeText(context, "Update Widget", Toast.LENGTH_SHORT).show();

WidgetActivity.Widgetke.mHandler.removeCallbacks(WidgetActivity.Widgetke.mUpdateTimeTask);
WidgetActivity.Widgetke.mHandler.postDelayed(WidgetActivity.Widgetke.mUpdateTimeTask, 100);
}

@Override
public IBinder onBind(Intent arg0) {
return null;
}
}

private Runnable mUpdateTimeTask = new Runnable() {
public void run() {

WidgetActivity.Widgetke.onUpdate(context, appWidgetManager, appWidgetIds);
WidgetActivity.Widgetke.mHandler.postAtTime(this, 1000);

}
};
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ibluekey"
android:versionCode="1"
android:versionName="1.0">


<uses-sdk android:minSdkVersion="4" />

<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
<receiver android:name=".Reciever" android:label="@string/app_name">
<intent-filter>
<action android:name="MyTestIntent" />
</intent-filter>
</receiver>
<receiver android:name="com.ibluekey.WidgetActivity" 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/widgetprovider" />
</receiver>

<service android:name="com.ibluekey.WidgetActivity$UpdateService" />
</application>

</manifest>

最佳答案

这是一个很棒的教程系列,用于在 Android 中创建小部件并定期更新它们。它从这里开始: http://android-er.blogspot.com/2010/10/simple-home-screen-app-widget-with.html

并以循环计时器/警报结束于此: http://android-er.blogspot.com/2010/10/app-widget-using-alarm-manager.html

基本上您需要在 list 中设置一些东西来监听更新小部件的广播 Intent 。它还展示了如何为小部件创建配置 Activity 。

要创建警报或重复事件,您还可以使用 AlarmManager 类。

关于x 时间后 Android 小部件更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8287796/

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