如何使用API​​ 22中的服务在主屏幕小部件中显示图像?

时间:2019-05-21 19:37:42

标签: android widget

我创建了一个包含几个ImageView和一个TextView的小部件。当使用API​​ 22在模拟器中运行它时,将出现“问题加载小部件”消息。但是它可以在22以上的API中正常工作。
我用android studio模拟器和genymotion对其进行了测试,但结果相同。

  

widget_info.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget_updating"
android:minHeight="40dp"
android:minWidth="320dp"
android:previewImage="@mipmap/ic_launcher"
android:resizeMode="vertical"
android:updatePeriodMillis="0">
</appwidget-provider>    
  

widget.xml

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <TextView
    android:id="@+id/txt_widget"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_margin="8dp"
    android:text="@string/appwidget_text"
    android:textColor="@android:color/transparent"
    android:textSize="24sp"
    android:fontFamily="@font/font"
    android:textStyle="bold|italic"/>
  <ImageView
    android:layout_centerInParent="true"
    android:id="@+id/imgDot"
    android:src="@drawable/dot"
    android:layout_width="30dp"
    android:layout_height="30dp" />
  <ImageView
    android:id="@+id/imgHour2"
    android:src="@drawable/no_0"
    android:layout_marginTop="-10dp"
    android:layout_toLeftOf="@id/imgDot"
    android:layout_alignTop="@id/imgDot"
    android:layout_width="50dp"
    android:layout_height="50dp" />
  <ImageView
    android:id="@+id/imgHour1"
    android:src="@drawable/no_0"
    android:layout_toLeftOf="@id/imgHour2"
    android:layout_alignTop="@id/imgHour2"
    android:layout_width="50dp"
    android:layout_height="50dp" />
  <ImageView
    android:id="@+id/imgMin1"
    android:src="@drawable/no_0"
    android:layout_toRightOf="@id/imgDot"
    android:layout_alignTop="@id/imgDot"
    android:layout_marginTop="-10dp"
    android:layout_width="50dp"
    android:layout_height="50dp" />
  <ImageView
    android:id="@+id/imgMin2"
    android:src="@drawable/no_0"
    android:layout_toRightOf="@id/imgMin1"
    android:layout_alignTop="@id/imgMin1"
    android:layout_width="50dp"
    android:layout_height="50dp" />

  </RelativeLayout>    
  

UpdateService.java

 public class UpdateService extends Service {

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

 @Override
 public void onCreate() {
    super.onCreate();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startMyOwnForeground();
    }
 }

 @Override
 public int onStartCommand(Intent intent, int flags, int startId) {

    String time = getCurrentDateTime();

    RemoteViews view = new RemoteViews(getPackageName(), R.layout.widget_updating);
    view.setTextViewText(R.id.txt_widget, time);
    view.setImageViewResource(R.id.imgHour1,R.drawable.no_2);
    view.setImageViewResource(R.id.imgHour2,R.drawable.no_2);
    view.setImageViewResource(R.id.imgMin1,R.drawable.no_4);
    view.setImageViewResource(R.id.imgMin2,R.drawable.no_7);
    view.setImageViewResource(R.id.imgDot,R.drawable.dot);
    ComponentName theWidget = new ComponentName(this, UpdatingWidget.class);
    AppWidgetManager manager = AppWidgetManager.getInstance(this);
    manager.updateAppWidget(theWidget, view);

    return super.onStartCommand(intent, flags, startId);
 }

 private String getCurrentDateTime() {
    Calendar c = Calendar.getInstance();
    int minute = c.get(Calendar.MINUTE);
    int hour = c.get(Calendar.HOUR_OF_DAY);
    String strHour, strMin;
    strHour = (hour < 10 ? "0" : "") + String.valueOf(hour);
    strMin = (minute < 10 ? "0" : "") + String.valueOf(minute);

    return strHour + ":" + strMin;
 }

 @RequiresApi(api = Build.VERSION_CODES.O)
 private void startMyOwnForeground() {
    String NOTIFICATION_CHANNEL_ID = "com.example.simpleapp";
    String channelName = "My Background Service";
    NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
    chan.setLightColor(Color.BLUE);
    chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    assert manager != null;
    manager.createNotificationChannel(chan);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
    Notification notification = notificationBuilder.setOngoing(true)
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .setContentTitle("App is running in background")
            .setPriority(NotificationManager.IMPORTANCE_MIN)
            .setCategory(Notification.CATEGORY_SERVICE)
            .build();
    startForeground(2, notification);
   }
 }    

如何解决此问题?
注意:我有ImageViews问题。没有它,这是我的小部件工作正常。

1 个答案:

答案 0 :(得分:0)

这是一个老问题,但我还想知道它的价值,最终在ImageView组件中将android:src替换为app:srcCompat

我使用的是SVG / Vector drawable,发现如果不使用app:srcCompat,我会在API 23上看到“问题加载小部件”。