Android Widget 的製作

來源:互聯網
上載者:User

標籤:

一、產生Widget控制項

1.寫一個類:MyWidget extends AppWidgetProvider

2.註冊: 

        <receiver android:name=".receiver.MyWidget">            <intent-filter>                <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>            </intent-filter>            <meta-data                    android:name="android.appwidget.provider"                    android:resource="@xml/appwidget_info"/>        </receiver>    

 

3.在 res/xml 下寫一個:appwidget_info.xml  

    <?xml version="1.0" encoding="utf-8"?>    <appwidget-provider        xmlns:android="http://schemas.android.com/apk/res/android"        android:minWidth="294dp"        android:minHeight="72dp"        android:updatePeriodMillis="1800000"        android:initialLayout="@layout/process_widget"        >    </appwidget-provider>

 

4.寫一個Widget相應的布局  process_widget.xml

 

二、事件 --- 用到PendingIntent和RemoteViews

1.PendingIntent:主要用來設定傳統型程式的相應方式。此對象可以有三種Intent方式,  

  1)開始一個新的Activity  

  2)發送一個廣播  

  3)開始一個service

2.RemoteViews:因為傳統型程式和app程式不是屬於一個進程,不能通過正常的操作控制項的方式來操作.       

            需要使用系統提供的RemoteViews對象來操作傳統型程式.在這個類中有設定APP Widget中控制項屬性的方法.

3.在MyWidget中:

    // 在第一個widget被拖到螢幕上時調用 --- 可以用來開啟後台服務    @Override    public void onEnabled(Context context) {        super.onEnabled(context);    }    // 在最後一個widget被移除的時候調用 --- 可以用來關閉後台服務    @Override    public void onDisabled(Context context) {        super.onDisabled(context);    }    // 在到達指定的更新時間之後或者向案頭添加App widget執行個體時調用 --- 可以用來重啟服務,防止服務被使用者關了    @Override    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {        super.onUpdate(context, appWidgetManager, appWidgetIds);    }

 

4.在UpdateWidgetService中:

    //設定更新的組件        ComponentName provider = new ComponentName(UpdateWidgetService.this, MyWidget.class);    //獲得RemoteViews對象     RemoteViews views = new RemoteViews(getPackageName(), R.layout.process_widget);    views.setTextViewText(R.id.textView_widget_process_count, "正在啟動並執行進程:" + SystemInfoUtils.getRunningProcessCount(getApplicationContext()) + "個");    //描述一個動作,由另外一個應用進程執行    //自訂一個廣播事件,殺死後台進程,廣播註冊時的action是com.baymax.detector.killall    Intent intent = new Intent();    intent.setAction("com.baymax.detector.killall");    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);    views.setOnClickPendingIntent(R.id.btn_clear, pendingIntent);    AppWidgetManager awm = AppWidgetManager.getInstance(this);    awm.updateAppWidget(provider, views);

 

Android Widget 的製作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.