DoAppWidget.uts 2.3 KB
Newer Older
lizhongyi_'s avatar
lizhongyi_ 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 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 81 82 83 84 85 86 87
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 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{
        console.log("周期更新函数被触发")
        // 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
        console.log("桌面组件被添加")
    }

    override onDisabled(_context: Context):void{
        // Enter relevant functionality for when the last widget is disabled
        console.log("桌面组件被移除")
    }

}




/**
 * 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)
		let widgetText = "当前股票价格为:13.25元"
		let uiRunnable = new UIRunnable(context,appWidgetManager,appWidgetId,widgetText)
		// 更新UI
		UTSAndroid.getUniActivity()!.runOnUiThread(uiRunnable)
		
	},500);

}