widget视图
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/Widget.MyWidget.AppWidget.Container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:theme="@style/Theme.MyWidget.AppWidgetContainer"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/img1"
></ImageView>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_gravity="fill_horizontal"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/b1"
android:text="上一页"
></Button>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/b3"
android:text="确认"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
></Button>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="下一页"
android:id="@+id/b2"
></Button>
</LinearLayout>
</LinearLayout>
widget xml配置文件
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/app_widget_description"
android:initialKeyguardLayout="@layout/new_app_widget3"
android:initialLayout="@layout/new_app_widget3"
android:minWidth="250dp"
android:minHeight="100dp"
android:previewImage="@drawable/example_appwidget_preview"
android:previewLayout="@layout/new_app_widget3"
android:resizeMode="horizontal|vertical"
android:targetCellWidth="6"
android:targetCellHeight="6"
android:updatePeriodMillis="86400000"
android:widgetCategory="home_screen" />
widget java文件
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
import com.example.mywidget.BroadcastReceiver.MyBroadcastReceiver;
import com.example.mywidget.R;
/**
* Implementation of App Widget functionality.
*/
public class NewAppWidget3 extends AppWidgetProvider {
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
Log.e("", "updateAppWidget: appWidgetId"+appWidgetId );
CharSequence widgetText = context.getString(R.string.appwidget_text);
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget3);
// views.setTextViewText(R.id.appwidget_text, widgetText);
Intent intent_click=new Intent(context, MyBroadcastReceiver.class);
intent_click.setAction("shangyiye");
intent_click.putExtra("typeNotifi", 1);
intent_click.putExtra("MESSAGE","上一页");
//按钮点击事件: PendingIntent.getBroadcast 第四个值注意
PendingIntent mPI = PendingIntent.getBroadcast(context,0,intent_click,0);
views.setOnClickPendingIntent(R.id.b1,mPI);
Intent intent_click1=new Intent(context, MyBroadcastReceiver.class);
intent_click1.setAction("xiayiye");
intent_click1.putExtra("typeNotifi", 2);
intent_click1.putExtra("MESSAGE","下一页");
//按钮点击事件:
PendingIntent mPI1= PendingIntent.getBroadcast(context,0,intent_click1,0);
//按钮b1 绑定点击事件
views.setOnClickPendingIntent(R.id.b2,mPI1);
//设置壁纸按钮
Intent intent_click2=new Intent(context, MyBroadcastReceiver.class);
intent_click2.setAction("bizhi");
intent_click2.putExtra("typeNotifi", 3);
intent_click2.putExtra("MESSAGE","下一页");
//按钮点击事件:
PendingIntent mPI2= PendingIntent.getBroadcast(context,0,intent_click2,0);
//按钮b1 绑定点击事件
views.setOnClickPendingIntent(R.id.b3,mPI2);
// 指示小部件管理器更新小部件
appWidgetManager.updateAppWidget(appWidgetId, views);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}
@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
}
相关的广播
import android.app.Application;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.WallpaperManager;
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.graphics.Bitmap;
import android.os.Build;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import com.example.mywidget.MyApplication;
import com.example.mywidget.R;
import com.example.mywidget.Util.BitmapUtils;
import com.example.mywidget.Util.HuncunUtil;
import com.example.mywidget.widget.NewAppWidget3;
import java.io.IOException;
import java.util.List;
public class MyBroadcastReceiver extends BroadcastReceiver {
private static int [] imgs={R.mipmap.songzuer1,R.mipmap.nezhazha,R.mipmap.ic_launcher_round,R.mipmap.jiashizhneg,R.mipmap.qidong1};
private int indexImg=0;
private static final String TAG ="MyBroadcastReceiver" ;
//更新通知栏
private static final String CHANNEL_ID = "id";
private String NotificationBroadcastRece="notificatopnType";
private MyApplication myApplication=new MyApplication();
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
indexImg= HuncunUtil.getDelegate(context,"indexImg");
int type = intent.getIntExtra("typeNotifi", -1);
Log.e(TAG, "onReceive: "+action );
if (action.equals("shangyiye")) {
Log.d(TAG, "上一页--"+indexImg);
indexImg=indexImg-1;
//处理点击事件
String message = intent.getStringExtra("MESSAGE");
updateView(context,type);
}else if (action.equals("xiayiye")){
indexImg=indexImg+1;
//处理点击事件
String message = intent.getStringExtra("MESSAGE");
updateView(context,type);
}else if (action.equals("bizhi")){
updateView(context,type);
}
}
//修改视图
public void updateView(Context context,int type){
if(indexImg<0){
indexImg=0;
Toast.makeText(context, "到底了", Toast.LENGTH_LONG).show();
}else if(indexImg==5){
indexImg=4;
Toast.makeText(context, "已经是最后一个了", Toast.LENGTH_LONG).show();
}
if(type==3){
Log.e(TAG, "updateView: 涉笔壁纸");
WallpaperManager mWallManager=WallpaperManager.getInstance(context);
try
{
mWallManager.setResource(imgs[indexImg]);
}
catch ( IOException e)
{
e.printStackTrace();
}
}else{
Log.d(TAG, "updateView: ---"+indexImg);
HuncunUtil.saveStringToSp(context,"indexImg",indexImg);
AppWidgetManager manager = AppWidgetManager.getInstance(context.getApplicationContext());//获得appwidget管理实例,用于管理appwidget以便进行更新操作
ComponentName componentName = new ComponentName(context.getApplicationContext(), NewAppWidget3.class);//获得所有本程序创建的appwidget
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.new_app_widget3);//获取远程视图
remoteViews.setTextViewText(R.id.b1,"上一页"+indexImg);
remoteViews.setTextViewText(R.id.b2,"下一页"+indexImg);
remoteViews.setImageViewResource(R.id.img1,imgs[indexImg]);
manager.updateAppWidget(componentName,remoteViews);
}
}
}
动态注册广播()
public void createBroadcast(){
Log.e("注册广播", "createBroadcast: 注册广播");
//注册广播
intentFilter = new IntentFilter();
intentFilter.addAction("aaaa");
intentFilter.addAction("aaaaa");
//创建广播接收器
myBroadcast = new MyBroadcastReceiver();
//动态注册广播接收器
registerReceiver(myBroadcast, intentFilter);
}
AndroidManifest 文件声明widget
<receiver
android:name=".widget.NewAppWidget3"
android:exported="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/new_app_widget3_info" />
</receiver>