我正在创建一个随机数的Android小部件..它工作正常..但我想在1秒后更新此数字...但它不可能..请帮助..这是我的代码。
public class MainActivity extends AppWidgetProvider {
public void onUpdate(Context context,AppWidgetManager appWidgetManager, int[] appWidgetIds){
super.onUpdate(context, appWidgetManager, appWidgetIds);
for(int i=0;i<appWidgetIds.length;i++){
int appWidgetId= appWidgetIds[i];
Intent intent=new Intent(context,MainActivity.class);
PendingIntent pending=PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews views=new RemoteViews(context.getPackageName(),R.layout.activity_main);
Random r=new Random();
int randomInt=r.nextInt(10);
String rand=String.valueOf(randomInt);
views.setTextViewText(R.id.textView1,rand);
final AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pending);
long interval = 1000;
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),interval, pending);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}
答案 0 :(得分:0)
我希望你可以解决这个问题,无论如何我只是粘贴我的代码
1)AppWidgetProvider
public class SimpleWidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int count = appWidgetIds.length;
for (int i = 0; i < count; i++) {
int widgetId = appWidgetIds[i];
String number = String.format("%03d", (new Random().nextInt(900) + 100));
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.simple_widget);
remoteViews.setTextViewText(R.id.textView, number);
Intent intent = new Intent(context, SimpleWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.actionButton, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals("com.aaa.bbb.REQUEST_WIDGET_UPDATE"))
{
//ComponentName, RemoteViews and AppWidgetManager
ComponentName provider = new ComponentName(context.getApplicationContext(), SimpleWidgetProvider.class);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.simple_widget);
AppWidgetManager manager = AppWidgetManager.getInstance(context.getApplicationContext());
//Change the TextView value
//String timeLeft = intent.getStringExtra("timeLeft");
String number = String.format("%03d", (new Random().nextInt(900) + 100));
views.setTextViewText(R.id.textView, number);
//Update the widget
manager.updateAppWidget(provider, views);
}
else
{
super.onReceive(context, intent);
}
}
}
2)MainActivity
公共类MainActivity扩展了AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BroadcastReceiver br = new SimpleWidgetProvider();
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
filter.addAction("com.aaa.bbb.REQUEST_WIDGET_UPDATE");
this.registerReceiver(br, filter);
new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
Intent intent = new Intent();
intent.setAction("com.aaa.bbb.REQUEST_WIDGET_UPDATE");
sendBroadcast(intent);
}
public void onFinish() {
Intent intent = new Intent();
intent.setAction("com.aaa.bbb.REQUEST_WIDGET_UPDATE");
sendBroadcast(intent);
}
}.start();
}
}
3)AndroidManifest.xml
<receiver android:name="SimpleWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.aaa.bbb.REQUEST_WIDGET_UPDATE"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/simple_widget_info" />
</receiver>