一个切换的Widget

因为是widget所以效果图不上了,直接看布局文件吧:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="108px"
    android:layout_height="124px"
    android:gravity="center"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/newwork_switch"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />

</LinearLayout>

在看看manifest文件吧:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xxx.networkswitch"
    android:versionCode="1"
    android:versionName="1.0"
    android:sharedUserId="android.uid.system" >
    <!-- <uses-sdk android:minSdkVersion="15" /> -->
  
    <uses-permission android:name="android.permission.WRITE_APN_SETTINGS"/>
   
    
    <application
        android:label="@string/app_name" >
        
        <receiver android:name=".NetworkSwitchWidgetProvider"
            android:icon="@drawable/net_press">
			<meta-data android:name="android.appwidget.provider"
				android:resource="@xml/appwidget_provider"></meta-data>
			<intent-filter>
				<action android:name="com.xxx.action.widget.click"></action>
				<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
				<action android:name="com.xxx.action.widget.network.changed" />
			</intent-filter>
		</receiver>

        </application>

</manifest>

好吧 看看重点
android:resource="@xml/appwidget_provider"></meta-data>


<?xml version="1.0" encoding="UTF-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
   android:minWidth="54dip"
    android:minHeight="62dip"
    android:updatePeriodMillis="0"
    android:previewImage="@drawable/net_press"
    android:initialLayout="@layout/main">
</appwidget-provider>


下面只能看看代码了:


package com.lenovo.networkswitch;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.telephony.MSimTelephonyManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.widget.RemoteViews;
import android.widget.Toast;
/**
 * 
 * @author dong xt 2012.3.17
 * Funtion : NetworkSwitch toggle
 * 
 */
public class NetworkSwitchWidgetProvider extends AppWidgetProvider {
	private static final String CLICK_NAME_ACTION = "com.xxx.action.widget.click";
	private static final String NETWORK_STATE_CHANGED = "com.xxx.action.widget.network.changed";

	public static final String PREFERRED_APN_URI =
	        "content://telephony/carriers/preferapn";
	private static final Uri PREFERAPN_URI = Uri.parse(PREFERRED_APN_URI);
	 
	private static RemoteViews remoteViews;
	private static final int ID_INDEX = 0;
	public static final String APN_ID = "apn_id";
	private static final String WAP = "389";
	private static final String NET = "388";
	private static String mSelectedKey;
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		// TODO Auto-generated method stub
		final int N = appWidgetIds.length;
		for (int i = 0; i < N; i++) {
			int appWidgetId = appWidgetIds[i];
			updateAppWidget(context, appWidgetManager, appWidgetId);
		}
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		super.onReceive(context, intent);

		if (remoteViews == null) {
			remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
		}
		mSelectedKey = getSelectedApnKey(context);
		if (intent.getAction().equals(CLICK_NAME_ACTION)) {
			if ((mSelectedKey != null) && mSelectedKey.equals(NET)) {
				setSelectedApnKey(WAP,context);
				remoteViews.setImageViewResource(R.id.newwork_switch, R.drawable.wap_network);

			} else 
				if ((mSelectedKey != null) && mSelectedKey.equals(WAP)){
					setSelectedApnKey(NET,context);
					remoteViews.setImageViewResource(R.id.newwork_switch, R.drawable.net_network);
			}
		}else if(intent.getAction().equals(NETWORK_STATE_CHANGED)){
			remoteViewsSetImageView(context);
		}else
			if(mSelectedKey == null || (!mSelectedKey.equals(NET) && !mSelectedKey.equals(WAP))){
			remoteViews.setImageViewResource(R.id.newwork_switch, R.drawable.disable);
			Toast.makeText(context, "网络切换不可用,请检查电话卡", Toast.LENGTH_SHORT).show();
		}
		AppWidgetManager appWidgetManger = AppWidgetManager
				.getInstance(context);
		int[] appIds = appWidgetManger.getAppWidgetIds(new ComponentName(
				context, NetworkSwitchWidgetProvider.class));
		appWidgetManger.updateAppWidget(appIds, remoteViews);
	}

	public void updateAppWidget(Context context,
			AppWidgetManager appWidgeManger, int appWidgetId) {
		
		
		remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
		MSimTelephonyManager mTelephonyManager = (MSimTelephonyManager)context.getSystemService(Context.MSIM_TELEPHONY_SERVICE);
		if(mTelephonyManager.getSimState(0) != mTelephonyManager.SIM_STATE_READY){
			remoteViews.setImageViewResource(R.id.newwork_switch, R.drawable.disable);
		}else{
			remoteViewsSetImageView(context);
			Intent intentclick = new Intent();
			ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.ApnSettings");
			intentclick.putExtra("subscription", 0);
			intentclick.setComponent(cn);
			
			PendingIntent pendingIntent = PendingIntent.getActivity(context,
	                0 /* no requestCode */, intentclick, 0 /* no flags */);
			remoteViews.setOnClickPendingIntent(R.id.newwork_switch, pendingIntent);
		}
		/*remoteViewsSetImageView(context);
		Intent intentClick = new Intent();
		intentClick.setAction(CLICK_NAME_ACTION);
		
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
				intentClick, 0);
		remoteViews.setOnClickPendingIntent(R.id.newwork_switch, pendingIntent);*/
		appWidgeManger.updateAppWidget(appWidgetId, remoteViews);
	}
	
	private void setSelectedApnKey(String key,Context context) {
       mSelectedKey = key;
       ContentResolver resolver = context.getContentResolver();

       ContentValues values = new ContentValues();
       values.put(APN_ID, mSelectedKey);
       resolver.update(PREFERAPN_URI, values, null, null);
   }
	private void remoteViewsSetImageView(Context context){
		mSelectedKey = getSelectedApnKey(context);
		if ((mSelectedKey != null) && mSelectedKey.equals(WAP)) {
			remoteViews.setImageViewResource(R.id.newwork_switch, R.drawable.wap_network);

		} else 
			if ((mSelectedKey != null) && mSelectedKey.equals(NET)){
			remoteViews.setImageViewResource(R.id.newwork_switch, R.drawable.net_network);
		}else
			if(mSelectedKey == null || (!mSelectedKey.equals(NET) && !mSelectedKey.equals(WAP))){
			remoteViews.setImageViewResource(R.id.newwork_switch, R.drawable.disable);
		}
	}
	private String getSelectedApnKey(Context context) {
	        String key = null;
	        Cursor cursor = context.getContentResolver().query(PREFERAPN_URI, new String[] {"_id"}, 
					null, null, "name ASC");
			if(cursor.getCount() > 0){
				cursor.moveToFirst();
				key = cursor.getString(ID_INDEX);
			}
			cursor.close();
	        return key;
	 }
}


你可能感兴趣的:(android,String,layout,null,NetWork,encoding)