note 34-AppWidgetProvider and deeper into PendingIntent

About pendingIntent:

 

1. PendingIntent is used in the situation that controling the remote process components . For example , to control an AppWidget, to control the notification.

 

2. Wraping an intent and waiting a condition to trigget the intent inside.

 

 

 

2. About remoteViews:

 

One remoteViews contains some child views . For exp, one Widget panel is a remoteView .

 

 

 

3.  AppWidgetProvider:

 

It extends BroadcastReceiver, so it must be registered in the manifest file , and set the intent-filter. What's more, it must with a meta-data with a default name

 

android:name="android.appwidget.provider"

 

and indicate the source xml

 

android:resource="@xml/exp_appwidget_info"

 

 

<receiver
            android:label="Hello,AppWidget"
            android:name="ExampleAppWidgetProvider">
                
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
            </intent-filter>
            
            <intent-filter>
                <action android:name="com.appwidget_test.ExampleAppWidgetProvider.ExpAction"/>
            </intent-filter>
            
            <meta-data android:name="android.appwidget.provider"
                       android:resource="@xml/exp_appwidget_info"/>                 
        </receiver>

 

The OnReceive function will trigger another 4 functions .

 

-onUpdate() ,

-onEnable(),

-onDisable(),

-onDelete();

 

so , when override the onReceive function ,MUST BE WITH A else branch to point to the super.onReceive(context, intent);

 

 

 

 

 

 

How to control the appWidget:

 

 

Log.i("l","onUpdate");
        
        //function 1 , get the remote activity start;
//        for(int i=0;i<appWidgetIds.length;i++){
//            Log.i("l","ids:"+appWidgetIds[i]);
//            
//            //1.set an intent use for the appwidget button
//            Intent intent=new Intent(context,TargetActivity.class);
//            //2.set an pendingIntent wraping the intent as the param of the 
//            //remoteView.setOnCliclPendingIntent(int targetViewId, PendingIntent pendingIntent);
//            PendingIntent pendingIntent=PendingIntent.getActivity(context, 0, intent, 0);
//            
//            //3. setup the remoteView,indicate the correspind layout
//            RemoteViews remoteViews=new RemoteViews(context.getPackageName(),R.layout.example_appwidget);
//            
//            //4. setOnClickPendingIntent binding the child view id
//            remoteViews.setOnClickPendingIntent(R.id.widgetButtonId, pendingIntent);
//            
//            //5. bind the remote view to the correspond appWidget id by 
//            //appWidgetManager.updateAppWidget(int appWidgetId, RemoteView remoteView) function
//            appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
//            
//            Log.i("l","end loop");
//        }
        
        
        
        
        //function 2 , broadcast an action
        
        //1.set an intent use for the appwidget button ,and set the action 
        //that registered in the manifest file
        Intent intent=new Intent();
        intent.setAction(EXP_ACTION);
        
        //2.set an pendingIntent wraping the intent as the param of the 
        //remoteView.setOnCliclPendingIntent(int targetViewId, PendingIntent pendingIntent);
        PendingIntent pendingIntent=PendingIntent.getBroadcast(context, 0, intent, 0);
        
        //3. setup the remoteView,indicate the correspind layout
        RemoteViews remoteViews=new RemoteViews(context.getPackageName(),
                R.layout.example_appwidget);
        
        //4. setOnClickPendingIntent binding the child view id
        remoteViews.setOnClickPendingIntent(R.id.widgetButtonId, pendingIntent);
        
        
        //5. bind the remote view to the correspond appWidget id by 
        //appWidgetManager.updateAppWidget(int appWidgetId, RemoteView remoteView) function
        //update all of the ids by another param int[] appWidgetIds.
        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
        
        super.onUpdate(context, appWidgetManager, appWidgetIds);

 

 

 

All  codes:

 

 

 

 

 

package com.appwidget_test;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
 

 

 

 */
package com.appwidget_test;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.LinearLayout;

/**
 *
 * @author Administrator
 */
public class TargetActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        
        Log.i("l","TargetActivity onCreate");
        
        LinearLayout ll=new LinearLayout(this);
        Button b=new Button(this);
        b.setText("button");
        
        ll.addView(b);
        
        this.setContentView(ll);
    }
    
    
    
}

 

 

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Hello World, App Widget"
    />
</LinearLayout>

 

 

 

 exp_appwidget_info.xml

 

<appwidget-provider
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="294dp"
    android:minHeight="72dp"
    android:updatePeriodMillis="86400000"
    android:initialLayout="@layout/example_appwidget"
    />

 

 

example_appwidget.xml:

 

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:id="@+id/widgetTextId"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="first widget text"
    android:background="#000000"
    />
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    <ImageView
        android:id="@+id/widgetViewId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/alert_dark_frame"
        />
    <Button
        android:id="@+id/widgetButtonId"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="App Widget Button"
        />    
</LinearLayout>

</LinearLayout>
 

 

 

 

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.appwidget_test;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.RemoteViews.RemoteView;

/**
 *
 * @author Administrator
 */
public class ExampleAppWidgetProvider extends AppWidgetProvider{
    
    private static final String EXP_ACTION="com.appwidget_test.ExampleAppWidgetProvider.ExpAction";
    
    

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
       
        Log.i("l","onUpdate");
        
        //function 1 , get the remote activity start;
//        for(int i=0;i<appWidgetIds.length;i++){
//            Log.i("l","ids:"+appWidgetIds[i]);
//            
//            //1.set an intent use for the appwidget button
//            Intent intent=new Intent(context,TargetActivity.class);
//            //2.set an pendingIntent wraping the intent as the param of the 
//            //remoteView.setOnCliclPendingIntent(int targetViewId, PendingIntent pendingIntent);
//            PendingIntent pendingIntent=PendingIntent.getActivity(context, 0, intent, 0);
//            
//            //3. setup the remoteView,indicate the correspind layout
//            RemoteViews remoteViews=new RemoteViews(context.getPackageName(),R.layout.example_appwidget);
//            
//            //4. setOnClickPendingIntent binding the child view id
//            remoteViews.setOnClickPendingIntent(R.id.widgetButtonId, pendingIntent);
//            
//            //5. bind the remote view to the correspond appWidget id by 
//            //appWidgetManager.updateAppWidget(int appWidgetId, RemoteView remoteView) function
//            appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
//            
//            Log.i("l","end loop");
//        }
        
        
        
        
        //function 2 , broadcast an action
        
        //1.set an intent use for the appwidget button ,and set the action 
        //that registered in the manifest file
        Intent intent=new Intent();
        intent.setAction(EXP_ACTION);
        
        //2.set an pendingIntent wraping the intent as the param of the 
        //remoteView.setOnCliclPendingIntent(int targetViewId, PendingIntent pendingIntent);
        PendingIntent pendingIntent=PendingIntent.getBroadcast(context, 0, intent, 0);
        
        //3. setup the remoteView,indicate the correspind layout
        RemoteViews remoteViews=new RemoteViews(context.getPackageName(),
                R.layout.example_appwidget);
        
        //4. setOnClickPendingIntent binding the child view id
        remoteViews.setOnClickPendingIntent(R.id.widgetButtonId, pendingIntent);
        
        
        //5. bind the remote view to the correspond appWidget id by 
        //appWidgetManager.updateAppWidget(int appWidgetId, RemoteView remoteView) function
        //update all of the ids by another param int[] appWidgetIds.
        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
        
        super.onUpdate(context, appWidgetManager, appWidgetIds);
         
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        super.onDeleted(context, appWidgetIds);
        Log.i("l","onDeleted");
    }

    @Override
    public void onDisabled(Context context) {
        super.onDisabled(context);
        Log.i("l","onDisable");
    }

    @Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
        Log.i("l","onEnable");
    }
    
    

    @Override
    public void onReceive(Context context, Intent intent) {
        
        
        if(intent.getAction().equals(EXP_ACTION)){
            Log.i("l","get the action in broadcast:"+intent.getAction());
            
            RemoteViews remoteViews=new RemoteViews(context.getPackageName(),
                    R.layout.example_appwidget);
            
            remoteViews.setImageViewResource(R.id.widgetViewId, android.R.drawable.alert_light_frame);
            remoteViews.setTextViewText(R.id.widgetTextId, "second text");
            
            AppWidgetManager appWidgetManager=AppWidgetManager.getInstance(context);
            
            ComponentName componentName=new ComponentName(context,ExampleAppWidgetProvider.class);
            appWidgetManager.updateAppWidget(componentName, remoteViews);
      
        }
        
        else{
            super.onReceive(context, intent);
        }
        
    }
    
    
    
}

 

 

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.appwidget_test"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:label="@string/app_name" >
        
        <activity android:name="MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <activity android:name="TargetActivity"
                  android:label="target activity">
                      
        </activity>
        
        <receiver
            android:label="Hello,AppWidget"
            android:name="ExampleAppWidgetProvider">
                
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
            </intent-filter>
            
            <intent-filter>
                <action android:name="com.appwidget_test.ExampleAppWidgetProvider.ExpAction"/>
            </intent-filter>
            
            <meta-data android:name="android.appwidget.provider"
                       android:resource="@xml/exp_appwidget_info"/>                 
        </receiver>
        
    </application>
</manifest> 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值