Android Widget點擊事件

來源:互聯網
上載者:User

在appWidget中,ImageButton和Button都是被支援的控制項,其事件可分成三種類型:
一、開啟Activity
二、開始Service
三、發送按鈕Action
下面開始一個一個分析,如何?。

一、開啟Activity
1、首先先定義個開啟Activity的intent
eg: 

Intent fullIntent=new Intent(this,FullScreen.class);
若要傳遞資料,則使用intent.putExtra()方法
eg:  fullIntent.putExtra("isCircle",isCircle);

2、用intent執行個體化一個PendingIntent,調用pendingIntent的getActicity方法來啟動另一個Activity
①若該Intent帶有資料,則需要將最後一個參數的值設為:FLAG_CANCEL_CURRENT
eg:  PendingIntent Pfullintent=PendingIntent.getActivity(this, 0, fullIntent,PendingIntent.FLAG_CANCEL_CURRENT);
②若該Intent不帶資料,則最後一個參數設為0
eg:  PendingIntent Pfullintent=PendingIntent.getActivity(this, 0, fullIntent, 0);

3、執行個體化RemoteView,其對應相應的Widget布局
eg:  RemoteViews views= newRemoteViews(getPackageName(), R.layout.widget);

4、給RemoteView上的Button或ImageButton設定按鈕事件
eg: views.setOnClickPendingIntent(R.id.IBfullscreen,Pfullintent);

5、更新AppWidget介面
①如果是在onUpdate()方法內更新AppWidget介面
eg: appWidgetManager.updateAppWidget(appWidgetIds, ActivityView);
②如果是在onUpdate()方法外(一般為Service內)更新AppWidget介面,則需要定義幾個變數
eg:  public RemoteViews views;//RemoteView對象
     publicComponentName thisWidget; //組件名
    public AppWidgetManager manager; // AppWidget管理器

    thisWidget = new ComponentName(this,PictureAppWidgetProvider.class);
    manager = AppWidgetManager.getInstance(this);
    manager.updateAppWidget(thisWidget, views);

二、開啟Service
1、定義一個intent來開啟Service
eg:  Intent startServiceInten=newIntent("zyf.temp.Service.START");
註:參數為開啟Service的動作

2、用Intent執行個體化一個PendingIntent,利用PendingIntent的getService方法來啟動一個服務
eg:  PendingIntent Pintent=PendingIntent.getService(context, 0, startServiceInten, 0);

3、執行個體化RemoteView,其對應相應的Widget布局
eg:  RemoteViews views= newRemoteViews(getPackageName(), R.layout.widget);

4、給RemoteView上的Button或ImageButton設定按鈕事件
eg: views.setOnClickPendingIntent(R.id.IBfullscreen,Pfullintent);

5、更新AppWidget介面
①如果是在onUpdate()方法內更新AppWidget介面
eg: appWidgetManager.updateAppWidget(appWidgetIds, ActivityView);
②如果是在onUpdate()方法外(一般為Service內)更新AppWidget介面,則需要定義幾個變數
eg:  public RemoteViews views;//RemoteView對象
     publicComponentName thisWidget; //組件名
    public AppWidgetManager manager; // AppWidget管理器

    thisWidget = new ComponentName(this,PictureAppWidgetProvider.class);
    manager = AppWidgetManager.getInstance(this);
    manager.updateAppWidget(thisWidget, views);

三、發送按鈕Action
1、定義一個Intent來發送按鈕Action
eg:  Intent prevInten=new Intent("PREV");

2、用Intent執行個體化一個PendingIntent,利用PendingIntent的getBroadcast方法來發送廣播
eg:  PendingIntent Pprevintent=PendingIntent.getBroadcast(this, 0, prevInten, 0);

3、執行個體化RemoteView,其對應相應的Widget布局
eg:  RemoteViews views= newRemoteViews(getPackageName(), R.layout.widget);

4、給RemoteView上的Button或ImageButton設定按鈕事件
eg:  views.setOnClickPendingIntent(R.id.IBprev,Pprevintent);

5、更新AppWidget介面
①如果是在onUpdate()方法內更新AppWidget介面
eg: appWidgetManager.updateAppWidget(appWidgetIds, ActivityView);
②如果是在onUpdate()方法外(一般為Service內)更新AppWidget介面,則需要定義幾個變數
eg:  public RemoteViews views;//RemoteView對象
     publicComponentName thisWidget; //組件名
    public AppWidgetManager manager; // AppWidget管理器

    thisWidget = new ComponentName(this,PictureAppWidgetProvider.class);
    manager = AppWidgetManager.getInstance(this);
    manager.updateAppWidget(thisWidget, views);

6、接收該Action
①在AppWidget自己的onReceive方法內接收
⒈在Action,要在Manifest.xml中加入Action
eg: <intent-filter>
     <actionandroid:name="android.appwidget.action.APPWIDGET_UPDATE"></action>
     <actionandroid:name="PREV"></action>
   </intent-filter>
⒉在onReceive()方法內編寫要實現的動作
eg:  if(intent.getAction().equals("PREV"))
{
   //在這編寫接收到該Action後要實現的動作
}
②在Service內接收
⒈註冊一個BroadcastReceive,聲明接收器
eg:  IntentFilter filter=new IntentFilter();
    filter.addAction("PREV");
    registerReceiver(doCommand, filter);
⒉,在BroadcastReceive類的onReceive方法內編寫要實現的動作
eg:  if(intent.getAction().equals("PREV"))
{
   //在這編寫接收到該Action後要實現的動作
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.