android 在自己的程序中添加Widget

package com.test;

import java.util.List;

import android.app.Activity;
import android.appwidget.AppWidgetHost;
import android.appwidget.AppWidgetHostView;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProviderInfo;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

public class AndroidImActivity extends Activity {
    
//	private WallpaperManager manager;
	
	private static final int APPWIDGET_HOST_ID = 1024;

	private AppWidgetHost  mAppWidgetHost ;
	
	private AppWidgetManager manager;
	
	
	
	int current;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        manager = AppWidgetManager.getInstance(this);
        mAppWidgetHost = new AppWidgetHost(this, APPWIDGET_HOST_ID);
        mAppWidgetHost.startListening();
        current   = mAppWidgetHost.allocateAppWidgetId();
//        manager = WallpaperManager.getInstance(this);
    }
    /*
    private void deCodeImage(){
    	InputStream in = getResources().openRawResource(R.drawable.logo);
    	Bitmap image = BitmapFactory.decodeStream(in);
    	int height = image.getHeight();
    	int width = image.getWidth();
    	image.getPixel(1, 1);
    }
    
    public void test(View v){
    	deCodeImage();
    	manager.setWallpaperOffsetSteps(1.0f / 2, 0);
    	IBinder windowToken = v.getWindowToken();
		manager.setWallpaperOffsets(windowToken ,  Math.max(0.f, Math.min(0.3f, 1.f)), 0);
//		manager.sendWallpaperCommand(windowToken, "android.wallpaper.tap", -1, -1, -1,null);
    	current++;
    	if(current ==3) current=0;
////    	try {
////			manager.clear();
////		} catch (IOException e) {
////			e.printStackTrace();
////		}
    	
    }*/
    
    public void test(View v){
//    	Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
    	 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
//    	 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId );
//    	startActivityForResult(intent, 101);
    	List<AppWidgetProviderInfo> insts = manager.getInstalledProviders();
    	for(AppWidgetProviderInfo info :insts){
    		if(info.provider.getClassName().equals("com.android.music.MediaAppWidgetProvider")){
    			manager.bindAppWidgetId(appWidgetId, info.provider);
    			break;
    		}
    	}
    	System.out.println(appWidgetId);
    	AppWidgetProviderInfo info =manager. getAppWidgetInfo(appWidgetId);
		 AppWidgetHostView view = mAppWidgetHost.createView(this, appWidgetId,info);
		 view.setAppWidget(appWidgetId, info);
		 view.setMinimumHeight(info.minHeight);
		 view.setMinimumWidth(info.minWidth);
		 LinearLayout l = (LinearLayout) findViewById(R.id.roots);
		 l.addView(view);
    }
    
    @Override
    	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    		System.out.println(requestCode +"==============="+ resultCode);
    		if(requestCode == 101){
    			int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
    			System.out.println(appWidgetId);
    			 AppWidgetProviderInfo info =manager. getAppWidgetInfo(appWidgetId);
    			 AppWidgetHostView view = mAppWidgetHost.createView(this, appWidgetId,info);
    			 view.setAppWidget(appWidgetId, info);
    			 view.setMinimumHeight(info.minHeight);
    			 view.setMinimumWidth(info.minWidth);
    			 LinearLayout l = (LinearLayout) findViewById(R.id.roots);
    			 l.addView(view);
    		}
    	}
}
 

你可能感兴趣的:(android)