如果你想让你的软件也可以有桌面小控件的话就让我们一起来学习下Widgets

First

Adding the AppWidgetProviderInfo Metadata

就是要在清单文件配置

App Widgets的使用_第1张图片

配置元数据

 android:name="ExampleAppWidgetProvider" >               android:name="android.appwidget.action.APPWIDGET_UPDATE" />           android:name="android.appwidget.provider"                android:resource="@xml/example_appwidget_info" /> 

Second

Adding the AppWidgetProviderInfo Metadata

The AppWidgetProviderInfo defines the essential qualities of an App Widget, such as its minimum layout dimensions, its initial layout resource, how often to update the App Widget, and (optionally) a configuration Activity to launch at create-time. Define the AppWidgetProviderInfo object in an XML resource using a single element and save it in the project's res/xml/ folder.


第二步在清单文件里元数据 需要去配置下然后在mata-data里有个 resource 然后我们在res下去创建xml :其实就是显示在桌面上的大小,还有设置刷新时间,还有其他相关设置。(

 android:initialLayout="@layout/example_appwidget" )是你想让你桌面图标长啥样我就设置了几个
<?xml version="1.0" encoding="utf-8"?><appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"    android:initialLayout="@layout/example_appwidget"    android:minHeight="72.0dip"    android:minWidth="294.0dip"    android:updatePeriodMillis="0" />
说下
updatePeriodMillis="0"这里填0是说在桌面上刷新时间由自己设定

initialLayout  就是你想让小控件长什么样

 xmlns:android="http://schemas.android.com/apk/res/android"     android:minWidth="40dp"     android:minHeight="40dp"     android:updatePeriodMillis="86400000"     android:previewImage="@drawable/preview"     android:initialLayout="@layout/example_appwidget"     android:configure="com.example.android.ExampleAppWidgetConfigure"      android:resizeMode="horizontal|vertical"     android:widgetCategory="home_screen|keyguard"     android:initialKeyguardLayout="@layout/example_keyguard"> 

 第三步需要去配置一个广播(就是用于检测你选哪个小窗体)   

public class MyAppWidgetProvider extends AppWidgetProvider {    /**     * 当前广播生命周期只有10秒     * @param context     */    public void onDisabled(Context context) {        super.onDisabled(context);        System.out.println("onDisabled");        Intent intent = new Intent(context, KillprocessWidgetService.class);        context.stopService(intent);    }    public void onEnabled(Context context) {        super.onEnabled(context);        System.out.println("onEnabled");        Intent intent = new Intent(context, KillprocessWidgetService.class);        context.startService(intent);    }}
//这里注意下我们如果耗时的操作就跳转到其他页面去操作因为广播周期一般就10左右。

第四步根据你的需求要让你的桌面小控件张啥样

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:background="@drawable/widget_bg_portrait"    android:gravity="center_vertical" >    <LinearLayout        android:layout_width="0.0dip"        android:layout_height="fill_parent"        android:layout_marginLeft="5.0dip"        android:layout_weight="1.0"        android:background="@drawable/widget_bg_portrait_child"        android:gravity="center_vertical"        android:orientation="vertical"        android:paddingBottom="3.0dip"        android:paddingTop="2.0dip" >        <TextView            android:id="@+id/process_counts"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="15.0dip"            android:textAppearance="@style/widget_text" />        <ImageView            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_marginBottom="1.0dip"            android:layout_marginTop="1.0dip"            android:background="@drawable/widget_bg_portrait_child_divider" />        <TextView            android:id="@+id/process_memorys"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="15.0dip"            android:textAppearance="@style/widget_text" />    LinearLayout>    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:gravity="center_horizontal"        android:orientation="vertical" >        <LinearLayout            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:gravity="center_vertical" >            <ImageView                android:layout_marginTop="3dp"                android:layout_width="20.0dip"                android:layout_height="20.0dip"                android:src="@drawable/logs" />            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="@string/app_name"                android:textColor="#ffffffff" />        LinearLayout>        <Button            android:id="@+id/btn_clear"            android:layout_width="90.0dip"            android:layout_height="wrap_content"            android:layout_centerVertical="true"            android:layout_marginTop="5.0dip"            android:background="@drawable/function_greenbutton_selector"            android:text="一键清理"            />    LinearLayout>LinearLayout>
写到这里从小窗口拖出到桌面就可以长成这样了。

实现相应功能的话我说下几个内容

1注意我们这是相当于一个程序app页面需要去展现到另一个程序上面 要的到view对象 是一个远程view

初始化远程view这和我们以前得到view 对象有所不同

 RemoteViews views = new RemoteViews(getPackageName(), R.layout.example_appwidget);
   

public class KillprocessWidgetService extends Service {    private AppWidgetManager widgetManager;    private TimerTask timerTask;    private TimerTask timerTask1;    private TimerTask timerTask11;    @Nullable    @Override    public IBinder onBind(Intent intent) {        return null;    }    @Override    public void onCreate() {        super.onCreate();        widgetManager = AppWidgetManager.getInstance(this);        //每隔5秒更新桌面        Timer timer=new Timer();        //初始化一个远程的view//第一个是一个上下文//第二个参数表示当前哪一个广播处理这个桌面小控件// System.out.println("晗宝宝");        timerTask11 = new TimerTask() {            public void run() {                //初始化一个远程的view                RemoteViews views = new RemoteViews(getPackageName(), R.layout.example_appwidget);                views.setTextViewText(R.id.process_counts,"正在运行的软件"+String.valueOf(SystemInfoUtils.getProcessCount(getApplicationContext())));                views.setTextViewText(R.id.process_memorys, "可用内存"+Formatter.formatFileSize(getApplicationContext(),SystemInfoUtils.getAvailMem(getApplicationContext())));                //第一个是一个上下文                //第二个参数表示当前哪一个广播处理这个桌面小控件                Intent intent = new Intent();                //发送一个隐式意图                intent.setAction("com.Meiko.safe");                PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);                //pendingIntent:to send when user clicks就是当用户点击时发送给谁                views.setOnClickPendingIntent(R.id.btn_clear,pendingIntent);                ComponentName proviede=new ComponentName(getApplicationContext(), MyAppWidgetProvider.class);               // System.out.println("晗宝宝");               widgetManager.updateAppWidget(proviede,views);            }        };        timer.schedule(timerTask11,0,5000);    }    @Override    public void onDestroy() {        super.onDestroy();    }}

更多相关文章

  1. Android 5.0新控件 AppBarLayout | 嵌套布局 介绍及使用详情
  2. RecyclerView Android RecyclerView 使用完全解析 体验艺术般的
  3. android 调用android系统的对话框控件
  4. 初识Android之(一)-自定义标题栏控件
  5. 基本控件学习二 (多选框)
  6. 控件_ProgressBar

随机推荐

  1. SQL on Hadoop 技术分析(二)
  2. Yarn【label-based scheduling】实战总结
  3. Yarn【label-based scheduling】实战总结
  4. BAT大佬带你了解AB测试
  5. Vue3 系统入门与项目实战
  6. 端午搬砖:聊聊调度云服务
  7. 原创 | 后疫情时代下5G安全的风险防控及
  8. 原创 | 工业网络通讯数据分析之Wireshark
  9. 闲聊乐视
  10. Cosmos DB的5种事物一致性