package com.linyb.widget_click_call;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import static android.content.ContentValues.TAG;
/**
* Created by lybly on 2018/3/1.
*/
public class AppWidgetService extends Service {
private static String ACTION_PIC="service.change.pic";
private final Intent updateIntent=new Intent(ACTION_PIC);
private static final int UPDATE_TIME=5000;
private UpdateThread mUpdateThread;
private Context mcontext;
private int count=0;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
startForeground(1,new Notification());
if (mUpdateThread!=null){
mUpdateThread.interrupt();
try {
mUpdateThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
mUpdateThread=new UpdateThread();
mUpdateThread.start();
mcontext=this.getApplicationContext();
super.onCreate();
}
@Override
public void onDestroy() {
if (mUpdateThread!=null){
mUpdateThread.interrupt();
try {
mUpdateThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Log.d(TAG, "onDestroy: ");
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
private class UpdateThread extends Thread{
@Override
public void run() {
super.run();
count=0;
while (true){
try {
Thread.sleep(UPDATE_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
count++;
Log.d(TAG, "run: "+count);
Intent intent=new Intent();
//不加setAction 8.0以上不行
intent.setAction("service.change.pic");
intent.setPackage(getPackageName()); //指定包名
mcontext.sendBroadcast(intent);
}
}
}
}
package com.linyb.widget_click_call;
import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import android.widget.RemoteViews;
import java.util.List;
import java.util.Random;
/**
* Implementation of App Widget functionality.
*/
//todo 马上删除马上创建不行
public class NewAppWidget extends AppWidgetProvider {
// 在最后一个 widget 被删除时,终止服务
private static String ACTION_PIC = "service.change.pic";
Random random = new Random();
private static final int[]PIC_LIST={
R.mipmap.img7,
R.mipmap.img8,
R.mipmap.img10,
R.mipmap.img11,
R.mipmap.img12,
};
public void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
// Construct the RemoteViews object
// RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
// ComponentName watchWidget = new ComponentName(context, NewAppWidget.class);
// Instruct the widget manager to update the widget
// appWidgetManager.updateAppWidget(watchWidget, 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) {
//打开服务
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
context.startForegroundService(new Intent(context, AppWidgetService.class));
} else {
context.startService(new Intent(context, AppWidgetService.class));
}
// 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
// 在最后一个 widget 被删除时,终止服务
//关闭服务,关闭时不能马上关闭的,要等
Intent intent = new Intent(context, AppWidgetService.class);
context.stopService(intent);
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
String action = intent.getAction();
//if (ACTION_PIC.equals(intent.getAction())) {
if (action.equals(ACTION_PIC)) {
Log.d(" ", "onReceive: ");
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
ComponentName watchWidget = new ComponentName(context, NewAppWidget.class);
remoteViews.setTextViewText(R.id.appwidget_text, "已经点击");
remoteViews.setImageViewResource(R.id.image_view,PIC_LIST[random.nextInt(PIC_LIST.length)] );
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
// Intent call_intent=new Intent(Intent.ACTION_DIAL, Uri.parse("tel:112" ));
// call_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// context.startActivity(call_intent);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.linyb.widget_click_call">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.linyb.widget_click_call.NewAppWidget">
<intent-filter>
<action android:name="service.change.pic"/>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/new_app_widget_info" />
</receiver>
<service android:name="com.linyb.widget_click_call.AppWidgetService">
</service>
</application>
</manifest>
关于onUpdate,有不少桌面在长按到桌面时候就开始加载widget,所以会开始计时(相对的,测试方法就是一直长按不懂,一会看看图片有没有变)