文章标签 ‘CMNET’
2011七月31

Android开发:休眠唤醒或开机后cmwap/cmnet网络不能连接的解决办法

Android手机(移动GSM)在休眠或开机后不能成功启用网络链接(设置都正常),有时候甚至状态栏图标是连接的,但网络依旧不可用。

如下解决方法,不知可通用,但测试HTC野火手机移动版可使用:

(被这个问题折腾死了,从本站相关文章可以看到,之前尝试了APN切换也不行,估计是网络enable==false)

package com.wiz.tools;

import java.lang.reflect.Method;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.telephony.TelephonyManager;
import android.util.Log;
/**
 * Wizzer.cn
 * @author Wizzer
 *
 */
public class NetCheck {
	public static final Uri APN_URI = Uri.parse("content://telephony/carriers");
	public static final Uri DEFAULTAPN_URI = Uri
			.parse("content://telephony/carriers/restore");
	public static final Uri CURRENT_APN_URI = Uri
			.parse("content://telephony/carriers/preferapn");
	public static Context c1;

	public static String getCurrentAPNFromSetting(ContentResolver resolver) {
		Cursor cursor = null;
		try {
			cursor = resolver.query(CURRENT_APN_URI, null, null, null, null);
			String curApnId = null;
			String apnName1 = null;
			if (cursor != null && cursor.moveToFirst()) {
				curApnId = cursor.getString(cursor.getColumnIndex("_id"));
				apnName1 = cursor.getString(cursor.getColumnIndex("apn"));
			}
			Log.e("NetCheck getCurrentAPNFromSetting", "curApnId:" + curApnId
					+ " apnName1:" + apnName1);
			// find apn name from apn list
			if (curApnId != null) {
				cursor = resolver.query(APN_URI, null, " _id = ?",
						new String[] { curApnId }, null);
				if (cursor != null && cursor.moveToFirst()) {
					String apnName = cursor.getString(cursor
							.getColumnIndex("apn"));
					return apnName;
				}
			}

		} catch (SQLException e) {
			Log.e("NetCheck getCurrentAPNFromSetting", e.getMessage());
		} finally {
			if (cursor != null) {
				cursor.close();
			}
		}

		return null;
	}

	public static int updateCurrentAPN(ContentResolver resolver, String newAPN) {
		Cursor cursor = null;
		try {
			// get new apn id from list
			cursor = resolver.query(APN_URI, null, " apn = ? and current = 1",
					new String[] { newAPN.toLowerCase() }, null);
			String apnId = null;
			if (cursor != null && cursor.moveToFirst()) {
				apnId = cursor.getString(cursor.getColumnIndex("_id"));
			}
			Log.e("NetCheck updateCurrentAPN", "apnId:" + apnId);
			// set new apn id as chosen one
			if (apnId != null) {
				ContentValues values = new ContentValues();
				values.put("apn_id", apnId);
				resolver.update(CURRENT_APN_URI, values, null, null);
			} else {
				// apn id not found, return 0.
				return 0;
			}
		} catch (SQLException e) {
			Log.e("NetCheck updateCurrentAPN", e.getMessage());
		} finally {
			if (cursor != null) {
				cursor.close();
			}
		}

		// update success
		return 1;
	}

	public String getApn(Context c) {
		boolean netSataus = false;

		ConnectivityManager conManager = (ConnectivityManager) c
				.getSystemService(Context.CONNECTIVITY_SERVICE);	
		if (conManager.getActiveNetworkInfo() != null) {
			netSataus = conManager.getActiveNetworkInfo().isAvailable();

		}
		NetworkInfo info = conManager
		.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		String oldAPN = StringUtils.null2String(info.getExtraInfo());
		Log
		.e("NetCheck getApn", "oldAPN:" + oldAPN + " netSataus:"
				+ netSataus);
		if (netSataus == false) {
			Log.e("NetCheck getApn", "setMobileDataEnabled(true)");
			setMobileDataEnabled(c, true);	

			try {
				Thread.sleep(4012);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		if("".equals(oldAPN)){			
			updateCurrentAPN(c.getContentResolver(), "cmnet");
			try {
				Thread.sleep(1500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		info = conManager
		.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		oldAPN = StringUtils.null2String(info.getExtraInfo());
		Log
				.e("NetCheck getApn", "newApn:" + oldAPN);
		return oldAPN.toLowerCase();
	}

	public boolean setMobileDataEnabled(Context c, boolean enabled) {
		final TelephonyManager mTelManager;
		mTelManager = (TelephonyManager) c
				.getSystemService(Context.TELEPHONY_SERVICE);
		try {

			Method m = mTelManager.getClass()
					.getDeclaredMethod("getITelephony");
			m.setAccessible(true);
			Object telephony = m.invoke(mTelManager);
			m = telephony.getClass().getMethod(
					(enabled ? "enable" : "disable") + "DataConnectivity");
			m.invoke(telephony);
			return true;
		} catch (Exception e) {
			Log.e("NetCheck ", "cannot fake telephony", e);
			return false;
		}
	}

}
2011七月15

Android 开发:APN网络切换之CMNET

最近被Android系统的APN自动切换网络问题折腾死了,软件使用CMNET网络,而系统自带的一些软件必须使用CMWAP,或者手机厂家搞的一些后台服务或者流氓软件总是在切换网络。没办法,只好想个解决之道了。

我的解决方案是:
1、在程序启动时,注册 Receiver 监视网络状态,当网络发生变化判断不是CMNET时则切换网络;
2、为了保险起见,在每个HTTP链接请求前加上网络判断。

本软件主要实现了功能如下:
拍照、定位、表单文件上传、查询、短信拦截(用于通过短信指令获得手机当前位置)、拨打电话、定时自动上传定位数据、版本更新等。

如下粘贴APN网络判断网站代码:
1、NetworkChangeReceiver  网络状态监视

package com.wiz.receiver;

import com.wiz.tools.NetCheck;
import com.wiz.tools.StringUtils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class NetworkChangeReceiver  extends BroadcastReceiver {
	NetCheck netCheck=new NetCheck();
	 public void onReceive(Context context, Intent intent) {
		 Log.e("NetworkChangeReceiver", "onReceive");
		 ConnectivityManager conManager= (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); 

        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
            NetworkInfo info = conManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            String apn = StringUtils.null2String(info.getExtraInfo());
            if (!"cmnet".equals(apn.toLowerCase())) {
            	netCheck.checkNetworkInfo(context);
            }
        }
    }
}

2、Activity 中注册 NetworkChangeReceiver

NetworkChangeReceiver ncr = new NetworkChangeReceiver();
IntentFilter upIntentFilter = new IntentFilter( ConnectivityManager.CONNECTIVITY_ACTION);
this.registerReceiver(ncr, upIntentFilter);// 网络状态监控

3、APN判断及网络切换

package com.wiz.tools;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.util.Log;

public class NetCheck {
	public static final Uri APN_URI = Uri.parse("content://telephony/carriers");
	public static final Uri CURRENT_APN_URI = Uri
			.parse("content://telephony/carriers/preferapn");
	public static String getCurrentAPNFromSetting(ContentResolver resolver) {
        Cursor cursor = null;
        try {
            cursor = resolver.query(CURRENT_APN_URI, null, null, null, null);
            String curApnId = null;
            String apnName1=null;
            if (cursor != null && cursor.moveToFirst()) {
                curApnId = cursor.getString(cursor.getColumnIndex("_id"));
                apnName1 = cursor.getString(cursor.getColumnIndex("apn"));
            }
            cursor.close();
            Log.e("NetCheck getCurrentAPNFromSetting","curApnId:"+curApnId+" apnName1:"+apnName1);
            //find apn name from apn list
            if (curApnId != null) {
                cursor = resolver.query(APN_URI, null, " _id = ?", new String[]{curApnId}, null);
                if (cursor != null && cursor.moveToFirst()) {
                    String apnName = cursor.getString(cursor.getColumnIndex("apn"));
                    return apnName;
                }
            } 

        } catch (SQLException e) {
            Log.e("NetCheck getCurrentAPNFromSetting",e.getMessage());
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        } 

        return null;
}
	public static int updateCurrentAPN(ContentResolver resolver, String newAPN) {
        Cursor cursor = null;
        try {
            //get new apn id from list
            cursor = resolver.query(APN_URI, null, " apn = ? and current = 1", new String[]{newAPN.toLowerCase()}, null);
            String apnId = null;
            if (cursor != null && cursor.moveToFirst()) {
                apnId = cursor.getString(cursor.getColumnIndex("_id"));
            }
            cursor.close();
            Log.e("NetCheck updateCurrentAPN","apnId:"+apnId);
            //set new apn id as chosen one
            if (apnId != null) {
                ContentValues values = new ContentValues();
                values.put("apn_id", apnId);
                resolver.update(CURRENT_APN_URI, values, null, null);
            } else {
                //apn id not found, return 0.
                return 0;
            }
        } catch (SQLException e) {
        	Log.e("NetCheck updateCurrentAPN",e.getMessage());
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        } 

        //update success
        return 1;
} 

	public boolean checkNetworkInfo(Context c) {
		boolean ret=false;
		ConnectivityManager conManager= (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = conManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		boolean internet=conManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
        String oldAPN = StringUtils.null2String(info.getExtraInfo());
        String oldSQLAPN=StringUtils.null2String(getCurrentAPNFromSetting(c.getContentResolver()));

        Log.e("NetCheck checkNetworkInfo","oldAPN:"+oldAPN+" oldSQLAPN:"+oldSQLAPN);
        if (internet==false||!"cmnet".equals(oldAPN.toLowerCase())||!"cmnet".equals(oldSQLAPN.toLowerCase())) {
            if("cmwap".equals(oldAPN.toLowerCase())&&"cmnet".equals(oldSQLAPN.toLowerCase())){
            	updateCurrentAPN(c.getContentResolver(), "cmwap");
            	try {
					Thread.sleep(200);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
            }
            updateCurrentAPN(c.getContentResolver(), "cmnet");
            try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
            ret=true;
        }
        return ret; 

	}
}

4、HTTP请求前的判断

if (nc.checkNetworkInfo(LoginActivity.this)) {
				Thread.sleep(5000);// 设置cmnet网络
			}

本文原创,转载请注明来源 wizzer.cn。

2011五月19

Android:设置APN为cmnet源码

public class APNActivity extends Activity {

        public static final Uri APN_URI = Uri.parse("content://telephony/carriers");
        public static final Uri CURRENT_APN_URI = Uri.parse("content://telephony/carriers/preferapn");

        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                int _cmnetId = addAPN();
                SetAPN(_cmnetId);
        }
       public void checkAPN(){
      // 检查当前连接的APN
              Cursor cr = getContentResolver().query(CURRENT_APN_URI, null, null,
              null, null);
              while (cr != null && cr.moveToNext()) {
                  // APN id
                  String id = cr.getString(cr.getColumnIndex("_id"));
                  // APN name
                  String apn = StringUtils.null2String(cr
                  .getString(cr.getColumnIndex("apn")));
                  // Toast.makeText(getApplicationContext(),
                  // "当前 id:" + id + " apn:" + apn, Toast.LENGTH_LONG).show();

       }

        //新增一个cmnet接入点
        public int addAPN() {
                int id = -1;
                ContentResolver resolver = this.getContentResolver();
                ContentValues values = new ContentValues();
                values.put("name", "cmnet");
                values.put("apn", "cmnet");
                Cursor c = null;
                Uri newRow = resolver.insert(APN_URI, values);
                if (newRow != null) {
                        c = resolver.query(newRow, null, null, null, null);
                        int idIndex = c.getColumnIndex("_id");
                        c.moveToFirst();
                        id = c.getShort(idIndex);
                }
                if (c != null)
                        c.close();
                return id;
        }
        //设置接入点
        public void SetAPN(int id) {
                ContentResolver resolver = this.getContentResolver();
                ContentValues values = new ContentValues();
                values.put("apn_id", id);
                resolver.update(CURRENT_APN_URI, values, null, null);
        }
}
2011五月18

Android:实用代码(开启启动、建立GPRS连接、闹钟等)

1:查看是否有存储卡插入

String status=Environment.getExternalStorageState();
if(status.equals(Enviroment.MEDIA_MOUNTED))
{
说明有SD卡插入
}

2:让某个Activity透明

OnCreate中不设Layout
this.setTheme(R.style.Theme_Transparent);

以下是Theme_Transparent的定义(注意transparent_bg是一副透明的图片)

3:在屏幕元素中设置句柄

使用Activity.findViewById来取得屏幕上的元素的句柄. 使用该句柄您可以设置或获取任何该对象外露的值.
TextView msgTextView = (TextView)findViewById(R.id.msg);
msgTextView.setText(R.string.push_me);

4:发送短信

String body=”this is mms demo”;

Intent mmsintent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(”smsto”, number, null));
mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body);
mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, true);
mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, true);
startActivity(mmsintent);

5:发送彩信

StringBuilder sb = new StringBuilder();

sb.append(”file://”);

sb.append(fd.getAbsoluteFile());

Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(”mmsto”, number, null));
// Below extra datas are all optional.
intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_SUBJECT, subject);
intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body);
intent.putExtra(Messaging.KEY_ACTION_SENDTO_CONTENT_URI, sb.toString());
intent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, composeMode);
intent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, exitOnSent);

startActivity(intent);

6:发送Mail

mime = “img/jpg”;
shareIntent.setDataAndType(Uri.fromFile(fd), mime);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(fd));
shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject);

shareIntent.putExtra(Intent.EXTRA_TEXT, body);

7:注册一个BroadcastReceiver

registerReceiver(mMasterResetReciever, new IntentFilter(”OMS.action.MASTERRESET”));

private BroadcastReceiver mMasterResetReciever = new BroadcastReceiver() {

public void onReceive(Context context, Intent intent){
String action = intent.getAction();
if(”oms.action.MASTERRESET”.equals(action)){
RecoverDefaultConfig();
}
}

};

8:定义ContentObserver,监听某个数据表

private ContentObserver mDownloadsObserver = new DownloadsChangeObserver(Downloads.CONTENT_URI);

private class DownloadsChangeObserver extends ContentObserver {
public DownloadsChangeObserver(Uri uri) {
super(new Handler());

}

@Override
public void onChange(boolean selfChange) {}
}

9:获得 手机UA

public String getUserAgent()
{
String user_agent = ProductProperties.get(ProductProperties.USER_AGENT_KEY, null);
return user_agent;
}

10:清空手机上Cookie

CookieSyncManager.createInstance(getApplicationContext());
CookieManager.getInstance().removeAllCookie();

11:建立GPRS连接

//Dial the GPRS link.
private boolean openDataConnection() {
// Set up data connection.
DataConnection conn = DataConnection.getInstance();

if (connectMode == 0) {
ret = conn.openConnection(mContext, “cmwap”, “cmwap”, “cmwap”);
} else {
ret = conn.openConnection(mContext, “cmnet”, “”, “”);
}

}

12:PreferenceActivity 用法

public class Setting extends PreferenceActivity

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}

Setting.xml:

Android:key=”seting2″
android:title=”@string/seting2″
android:summary=”@string/seting2″/>

android:key=”seting1″
android:title=”@string/seting1″
android:summaryOff=”@string/seting1summaryOff”
android:summaryOn=”@stringseting1summaryOff”/>

13:通过HttpClient从指定server获取数据

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet method = new HttpGet(“http://www.baidu.com/1.html”);
HttpResponse resp;
Reader reader = null;
try {
// AllClientPNames.TIMEOUT
HttpParams params = new BasicHttpParams();
params.setIntParameter(AllClientPNames.CONNECTION_TIMEOUT, 10000);
httpClient.setParams(params);
resp = httpClient.execute(method);
int status = resp.getStatusLine().getStatusCode();

if (status != HttpStatus.SC_OK) return false;

// HttpStatus.SC_OK;
return true;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (reader != null) try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

14:显示toast

Toast.makeText(this._getApplicationContext(), R.string._item, Toast.LENGTH_SHORT).show();

15:屏幕显示

程序中默的显示是带有标题栏和系统信息栏的,有的时候,这很影响程序界面的美观。手机默认的是竖屏,或与感应器状态相关,为了某种效果,我们的程序需要限制使用横屏或竖屏。以下的代码就解决了上述问题。

//设置为无标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
//设置为全屏模式
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//设置为横屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

16:Intent传参

当Activity与Activity/Service(或其它情况)有时与要进行参数传递,最常用也是最简单的方式就是通过Intent来处理。
看如下代码:
Intent intent = new Intent(…);
Bundle bundle = new Bundle();
bundle.putString(“NAME”, “zixuan”);
intent.putExtras(bundle);
context.startActivity(intent); 或 context.startService(intent);

当然,有传送就有接收,接收也很简单,如:
Bundle bunde = intent.getExtras();
String name = bunde.getInt(“NAME”);
当然参数KEY要与传送时的参数一致。

17:获取手机号

在j2me中,根本没有办法获取用户的手机号码,就连获取手机串号(IMEI)都基本上无法实现,然后在android手机上一切都是如此的简单,看代码:
TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String imei = tm.getDeviceId();
String tel = tm.getLine1Number();
看来,android的确加速了j2me的消亡。

18:振动器

总感觉手机上的振动器没有多大用处(当然静音模式下的振铃很有用),但还是顺带着说一下吧,只有两行代码:
1、获取振动服务的实例
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
2、设置振动时长,单位当然也是ms
vibrator.vibrate(1000);
如果你觉得这样过去单调的话,可以设个节奏:
vibrator.vibrate(new long[]{10, 100, 20, 200}, -1);
两个参数,习惯告诉我第一个是节奏,第二个是重复次数,可事实并没有这么简单,我翻译不好,大家还是看原文吧:
public void vibrate (long[] pattern, int repeat)
pattern: an array of longs of times to turn the vibrator on or off.
repeat: the index into pattern at which to repeat, or -1 if you don’t want to repeat.
google喜欢弄些技巧,我却觉得这里有点弄巧成拙了。

19:闹钟管理

最近看了一下Android的闹钟管理类(AlarmManager),真不错误,强大又简单,代码如下:

1)、建立一个AlarmReceiver继承入BroadcastReceiver,并在AndroidManifest.xml声明
public static class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, “闹钟提示:时间到!”, Toast.LENGTH_LONG).show();
}
}

2)、建立Intent和PendingIntent,来调用目标组件。
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

3)、设置闹钟
获取闹钟管理的实例:
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
设置单次闹钟:
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (5*1000), pendingIntent);
设置周期闹钟:
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10*1000), (24*60*60*1000), pendingIntent);

20:开机自启动

1).定义一个BroadcastReceiver

public class BootReceiver extends BroadcastReceiver {
public void onReceive(Context ctx, Intent intent) {
Log.d(“BootReceiver”, “system boot completed”);
//start activity
String action=”android.intent.action.MAIN”;
String category=”android.intent.category.LAUNCHER”;
Intent myi=new Intent(ctx,CustomDialog.class);
myi.setAction(action);
myi.addCategory(category);
myi.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(myi);
//start service
Intent s=new Intent(ctx,MyService.class);
ctx.startService(s);
}
}

2).配置Receiver的许可,允许接收系统启动消息,在AndroidManifest.xml中:

3).配置Receiver,可以接收系统启动消息,在AndroidManifest.xml中






4).启动模拟器,可以看到系统启动后,弹出一个对话框。

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/lxh2808/archive/2010/10/30/5976351.aspx