Android应用小部件appwidget开发全部代码案例和说明

在res目录下创建xml文件目录


 //home_screen|keyguard  主屏和锁屏

以下是全部代码

WidgetActivity.java

package com.example.a20200712;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.RemoteViews;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.text.SimpleDateFormat;
import java.util.Date;

public class WidgetActivity extends AppWidgetProvider {

    /**
     * 创建同类型的实例每次都会执行一次,
     * @param context
     * @param appWidgetManager
     * @param appWidgetIds
     */
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        Log.i("mw","onUpdate");
        //=======================只执行了一次时间显示================
        /*
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(new Date());

        RemoteViews rv = new RemoteViews(context.getPackageName(),R.layout.time_widget_layout);
        rv.setTextViewText(R.id.widget_layout_show_time,time);
        //这里只更新第一个widget实例
        appWidgetManager.updateAppWidget(appWidgetIds[0],rv);
        */
    }


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

    //创建同类型的实例只执行一次,即使后面再多次新增实例也不再执行
    @Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
        Log.i("mw","首次新增onEnabled");
        context.startService(new Intent(context,WidgetServiceImpl.class));
    }

    //删除同类型的实例每删除一次就会执行一次
    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        super.onDeleted(context, appWidgetIds);
        Log.i("mw","删除onDeleted");
    }

    //删除同类型的实例最后一个实例时,执行一次,意思是就只执行一次
    @Override
    public void onDisabled(Context context) {
        super.onDisabled(context);
        Log.i("mw","全部删除onDisabled");
        context.stopService(new Intent(context,WidgetServiceImpl.class));
    }
}
WidgetServiceImpl.java
package com.example.a20200712;

import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;

import androidx.annotation.Nullable;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class WidgetServiceImpl extends Service {
    ScheduledExecutorService scheduledExecutorService;


    //点击事件
    @Override
    public void onCreate() {
        Log.i("mw","创建Service");
        super.onCreate();
        scheduledExecutorService = Executors.newScheduledThreadPool(1);
        scheduledExecutorService.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String time = sdf.format(new Date());

                RemoteViews rv = new RemoteViews(getPackageName(),R.layout.time_widget_layout);
                rv.setTextViewText(R.id.widget_layout_show_time,time);

                PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),1,new Intent(getApplicationContext(),MainActivity.class),0);
                rv.setOnClickPendingIntent(R.id.widget_layout_show_time,pendingIntent);

                AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
                //这里更新所有widget同类型的实例
                appWidgetManager.updateAppWidget(new ComponentName(getApplicationContext(),WidgetActivity.class),rv);

            }
        },0,1000, TimeUnit.MILLISECONDS);
    }

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

    @Override
    public void onDestroy() {
        if(scheduledExecutorService!=null){

            scheduledExecutorService.shutdown();
            scheduledExecutorService = null;
            Log.i("mw","注消scheduledExecutorService");
        }
        super.onDestroy();
    }
}

res/layout/time_widget_layout.xml



    

 

res/xml/widget_layout.xml



AndroidManifest.xml配置

这里的

  ---》这个地方写你的widget配置

            
                
            
            
        

        
            
                
            
        

UI 方面大家自己再优化

Android应用小部件appwidget开发全部代码案例和说明_第1张图片

Android应用小部件appwidget开发全部代码案例和说明_第2张图片

Android应用小部件appwidget开发全部代码案例和说明_第3张图片

 

你可能感兴趣的:(Android,widget,android)