android典型程式碼系列(二十六)------App widget的使用
App widget的使用:
第一步、建立widget類
package cn.beita.mobilesafe.receiver;
import cn.beita.mobilesafe.service.AppWightService;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
public class AppWightReceiver extends AppWidgetProvider {
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
Intent intent=new Intent(context,AppWightService.class);
context.stopService(intent);
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
Intent intent=new Intent(context,AppWightService.class);
context.startService(intent);
System.out.println("Intent intent=new Intent(context,AppWightService.class);");
}
}
第二步、在清單檔案中進行配置
<receiver android:name=".receiver.AppWightReceiver" >
<intent-filter >
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/example_appwidget_info" />
</receiver>
第三步、xml— example_appwidget_info
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="294dp"
android:minHeight="72dp"
android:updatePeriodMillis="0"
android:initialLayout="@layout/example_appwidget">
</appwidget-provider>
第四步、建立佈局檔案:layout---example_appwidget
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@drawable/widget_bg_portrait"
android:gravity="center_vertical|center_horizontal">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/widget_bg_portrait_child"
android:orientation="vertical"
android:gravity="center_vertical"
>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginTop="3dip"
android:layout_marginBottom="3dip"
android:id="@+id/tv_widget_count"
android:text="程式數目"/>
<ImageView android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="@drawable/widget_bg_portrait_child_divider"/>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_widget_memeory"
android:layout_marginLeft="10dip"
android:layout_marginTop="3dip"
android:layout_marginBottom="3dip"
android:text="可用記憶體"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView android:layout_width="20dip"
android:layout_height="20dip"
android:src="@drawable/notification"
android:id="@+id/iv_appname"
android:layout_margin="4dip"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_appname"
android:layout_toRightOf="@id/iv_appname"
android:layout_alignBaseline="@id/iv_appname"
android:layout_marginTop="5dip"
android:text="手機衛士"/>
</RelativeLayout>
<Button android:layout_width="wrap_content"
android:layout_marginLeft="10dip"
android:paddingLeft="5dip"
android:paddingRight="10dip"
android:id="@+id/bt_example_appwidget"
android:layout_height="wrap_content"
android:background="@drawable/button_shape"
android:text="一鍵清理"/>
</LinearLayout>
</LinearLayout>
第五步、建立service時時地更新上面的appwidget
package cn.beita.mobilesafe.service;
public class AppWightService extends Service {
private AppWidgetManager widgetManager;
private ScheduledExecutorService pool;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
widgetManager=AppWidgetManager.getInstance(getApplicationContext());
System.out.println("widgetManager=AppWidgetManager.ge");
pool = Executors.newScheduledThreadPool(1);
pool.scheduleAtFixedRate(new Runnable(){
@Override
public void run() {
ComponentName componentName=new ComponentName("cn.beita.mobilesafe", "cn.beita.mobilesafe.receiver.AppWightReceiver");
RemoteViews view = new RemoteViews("cn.beita.mobilesafe", R.layout.example_appwidget);
view.setTextViewText(R.id.tv_widget_count, "當前程式數為:"+TaskUtils.getAllProcess(AppWightService.this));
view.setTextColor(R.id.tv_widget_count, Color.RED);
view.setTextViewText(R.id.tv_widget_memeory, "當前可用記憶體為:"+TextFormat.getDataSize(TaskUtils.getAvailMemeoty(AppWightService.this)));
view.setTextColor(R.id.tv_widget_memeory, Color.RED);
Intent intent=new Intent(AppWightService.this,LockClearReceiver.class);
PendingIntent pendingIntent=PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
view.setOnClickPendingIntent(R.id.bt_example_appwidget, pendingIntent);
widgetManager.updateAppWidget(componentName, view);
}
}, 1, 2, TimeUnit.SECONDS);
}
@Override
public void onDestroy() {
super.onDestroy();
pool.shutdown();
}
}
相關文章
- 把你的程式放到桌面——Android桌面部件WidgetAndroid
- Flutter系列二:探究Flutter App在Android宿主App中的整合FlutterAPPAndroid
- 創造無限可能 | 在 Android 12 中使用 widgetAndroid
- Android 網路程式設計系列(4)使用 HttpUrlConnectionAndroid程式設計HTTP
- Spring Boot入門系列(二十六)超級簡單!Spring Data JPA 的使用!Spring Boot
- Android Apk反編譯系列教程(三)Android Studio除錯smali程式碼AndroidAPK編譯除錯
- 深度解析Android APP加固中的必備手段——程式碼混淆技術AndroidAPP
- Java程式碼加密支援Android App Bundle動態化框架,守護核心程式碼安全Java加密AndroidAPP框架
- 使用GDB除錯Android Native 層程式碼除錯Android
- 典型的ETL使用場景
- Context的典型使用場景Context
- 如何開發APP:使用免費無程式碼製作APP工具APP
- 學習ASP.NET Core Blazor程式設計系列二十六——登入(5)ASP.NETBlazor程式設計
- android widget 開發例項 : 桌面便籤程式的實現詳解和源Android
- diffusers-原始碼解析-二十六-原始碼
- android使用kivy整合python的youtube-dl程式碼筆記AndroidPython筆記
- 使用 Upsource 實現程式碼審查 - jetbrains 系列AI
- Android Jetpack系列——ViewModel原始碼分析AndroidJetpackView原始碼
- Mac Jenkins 構建 Android App 時動態設定程式碼引數MacJenkinsAndroidAPP
- Android多程式之Binder的使用Android
- Android多程式之Messenger的使用AndroidMessenger
- Android 多程式之Messenger的使用AndroidMessenger
- 程式設計師笑話二十六程式設計師
- 淺析低程式碼開發的典型應用構建場景
- IOS小元件(8):App與Widget資料共享iOS元件APP
- 用Flutter做APP學習心得:Flutter widget的生命週期FlutterAPP
- 【Android Studio】入門系列 3.建或匯入APPAndroidAPP
- 直播app原始碼開源,Android 滾動的公告欄APP原始碼Android
- app直播原始碼,android中幾種常用的彈框APP原始碼Android
- Android 7 原始碼分析系列導讀Android原始碼
- 如何編寫高效的Android程式碼Android
- android HAL層程式碼Android
- 下載Android程式碼Android
- Android UsbDeviceManager 程式碼分析Androiddev
- 優化使用kotlin開發Android app的編譯速度優化KotlinAndroidAPP編譯
- 使用Kotlin高效地開發Android App(二)KotlinAndroidAPP
- 使用Kotlin高效地開發Android App(四)KotlinAndroidAPP
- 使用Kotlin高效地開發Android App(三)KotlinAndroidAPP
- 使用Kotlin高效地開發Android App(一)KotlinAndroidAPP