為什么我的那個(gè)組件顯示不出來,好奇怪,啊啊。。。。
package com.example.widget;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.IBinder;
import android.widget.RemoteViews;
public class TimerService extends Service {
private Timer timer;
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
updateViews();
}
}, 0, 1000);
}
private void updateViews() {
String time ?=sdf.format(new Date());
RemoteViews rv ?= new RemoteViews(getPackageName(),R.layout.widget);
rv.setTextViewText(R.id.tv, time);
AppWidgetManager manager = AppWidgetManager.getInstance(getApplicationContext());
ComponentName cn = new ComponentName(getApplicationContext(), WidgetProvider.class);
manager.updateAppWidget(cn, rv);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
timer = null;
}
}
2016-08-25
要這樣寫才有的
<receiver android:name="com.charspan.widgetdemo.WidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widgetconfig" />
? ? ? ?</receiver>
2018-07-08
還是出不來我用我自己的手機(jī)測(cè)試的