DoAppWidget.uts 2.3 KB
Newer Older
杜庆泉's avatar
杜庆泉 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
import AppWidgetProvider from 'android.appwidget.AppWidgetProvider';
import Context from 'android.content.Context';
import AppWidgetManager from 'android.appwidget.AppWidgetManager';
import RemoteViews from 'android.widget.RemoteViews';
import Handler from 'android.os.Handler';
import R from 'io.dcloud.uni_modules.uts_nativepage.R';

export class DoAppWidget extends AppWidgetProvider {
	
	constructor(){
		super()
		console.log("DoAppWidget")
	}
	
	
    override onUpdate(
        context: Context,
        appWidgetManager: AppWidgetManager,
        appWidgetIds: IntArray
    ):void{
lizhongyi_'s avatar
lizhongyi_ 已提交
21
        console.log("周期更新函数被触发")
杜庆泉's avatar
杜庆泉 已提交
22 23 24 25 26 27 28 29
        // There may be multiple widgets active, so update all of them
        for (appWidgetId in appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId)
        }
    }

    override onEnabled(_context: Context):void{
        // Enter relevant functionality for when the first widget is created
lizhongyi_'s avatar
lizhongyi_ 已提交
30
        console.log("桌面组件被添加")
杜庆泉's avatar
杜庆泉 已提交
31 32 33 34
    }

    override onDisabled(_context: Context):void{
        // Enter relevant functionality for when the last widget is disabled
lizhongyi_'s avatar
lizhongyi_ 已提交
35
        console.log("桌面组件被移除")
杜庆泉's avatar
杜庆泉 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    }

}




/**
 * Dialog ui任务封装
 */
class UIRunnable extends Runnable {
	
	context: Context;
	appWidgetManager: AppWidgetManager;
	appWidgetId: Int;
	widgetText:string;
	constructor(contextP:Context,appWidgetManagerP: AppWidgetManager,appWidgetIdP: Int,widgetTextP:string){
		super();
		this.context = contextP
		this.appWidgetManager = appWidgetManagerP
		this.appWidgetId = appWidgetIdP
		this.widgetText = widgetTextP;
	}


    override run() {
		let views = new RemoteViews(this.context.packageName, R.layout.do_app_widget)
		views.setTextViewText(R.id.appwidget_text_sub, this.widgetText)
		// Instruct the widget manager to update the widget
		this.appWidgetManager.updateAppWidget(this.appWidgetId, views)

    }
};



 function updateAppWidget(
    context: Context,
    appWidgetManager: AppWidgetManager,
    appWidgetId: Int
) {

	setTimeout(function(){
		// 模拟请求网络,这里需要替换成真正的网络请求
		Thread.sleep(1000)
81
		let widgetText = "当前股票价格为:13.25元"
杜庆泉's avatar
杜庆泉 已提交
82 83 84 85 86 87 88
		let uiRunnable = new UIRunnable(context,appWidgetManager,appWidgetId,widgetText)
		// 更新UI
		UTSAndroid.getUniActivity()!.runOnUiThread(uiRunnable)
		
	},500);

}