文章标签 ‘Android’
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五月28

Android:指定分辨率和清晰度的图片压缩方法源码

public void transImage(String fromFile, String toFile, int width, int height, int quality)
	{
		try
		{
			Bitmap bitmap = BitmapFactory.decodeFile(fromFile);
			int bitmapWidth = bitmap.getWidth();
			int bitmapHeight = bitmap.getHeight();
			// 缩放图片的尺寸
			float scaleWidth = (float) width / bitmapWidth;
			float scaleHeight = (float) height / bitmapHeight; 
			Matrix matrix = new Matrix();
			matrix.postScale(scaleWidth, scaleHeight);
			// 产生缩放后的Bitmap对象
			Bitmap resizeBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, false);
			// save file
			File myCaptureFile = new File(toFile);
			FileOutputStream out = new FileOutputStream(myCaptureFile);
			if(resizeBitmap.compress(Bitmap.CompressFormat.JPEG, quality, out)){
				out.flush();
				out.close();
			}
			if(!bitmap.isRecycled()){
				bitmap.recycle();//记得释放资源,否则会内存溢出
			}
			if(!resizeBitmap.isRecycled()){
				resizeBitmap.recycle();
			}

		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (IOException ex)
		{
			ex.printStackTrace();
		}
	}
2011五月19

Android:解决ProgressDialog提示框不转动

ProgressDialog 解决“第一次执行图标转动,第二次执行不转动”代码:

@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case PROGRESS_DIALOG:
			progressDialog = new ProgressDialog(LoginActivity.this);
			progressDialog.setMessage("正在登陆,请稍等...");
			progressDialog.setCancelable(true);
			// 设置ProgressDialog 是否可以按退回按键取消
			return progressDialog;
		default:
			return null;
		}
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		switch (id) {
		case PROGRESS_DIALOG:
			dialog
					.setOnDismissListener(new DialogInterface.OnDismissListener() {
						@Override
						public void onDismiss(DialogInterface dialog) {
							removeDialog(PROGRESS_DIALOG);//这个起作用
							if (progressThread != null) {
								progressThread = null;
							}
						}
					});
		}
	}
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

2011五月4

Android 手机端与服务端POST数据交互类

 

package com.wizzer.tools;

import java.io.*;
import java.net.URLEncoder;

import java.util.*;

import org.apache.http.HttpResponse;

import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;

public class BaseProtocol 
{
	private StringBuilder sb = new StringBuilder();
	private HttpClient httpClient;
	private HttpPost httpRequest;
	private HttpResponse response;
	private List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
	private static final int DIALOG1_KEY = 0;
	private static final int DIALOG2_KEY = 1;

	public BaseProtocol()
	{
		httpClient = new DefaultHttpClient();
	}

	/**
	 * *向服务器端发送请求 * *@paramurl *@throwsException
	 * 
	 * @throws UnsupportedEncodingException
	 */
	public void pack(String url) throws Exception
	{
		httpClient = new DefaultHttpClient();
		httpRequest = new HttpPost(url);
		httpRequest.setEntity(new UrlEncodedFormEntity(nameValuePair));
		response = httpClient.execute(httpRequest);
	}

	/** *得到返回数据 * *@paramurl *@return *@throwsException */

	public String parse() throws Exception
	{
		// TODO状态处理500200
		if (response.getStatusLine().getStatusCode() == 200)
		{
			BufferedReader bufferedReader2 = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
			for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2.readLine())
			{
				sb.append(s);
			}
		}
		return sb.toString();
	}

	/***
	 * 向服务器发送信息 * *@paramkey *@paramvalue
	 * 
	 * @throws UnsupportedEncodingException
	 */
	public void addNameValuePair(String key, String value) throws UnsupportedEncodingException
	{
		nameValuePair.add(new BasicNameValuePair(key, URLEncoder.encode(value, HTTP.UTF_8)));
	}

	/** *返回JSONArray对象数据模型 * *@return *@throwsJSONException */

	public JSONArray getJSON() throws JSONException
	{
		return new JSONArray(sb.toString());
	}

}
2011五月4

合肥 Android/Java开发交流群:26310065

手机开发:WM、Android、iOS
WEB开发:Java、.NET、ASP、PHP、JSP
应用开发:C#、JAVA……

欢迎IT从业人士加入,交流开发经验、技术,项目外包等信息。
QQ群:26310065

2011四月1

Android开发:屏幕常亮 PowerManager和PowerManager.WakeLock

前言

  学习android一段时间了,为了进一步了解android的应用是如何设计开发的,决定详细研究几个开源的android应用。从一些开源应用中吸收点东西,一边进行量的积累,一边探索android的学习研究方向。这里我首先选择了jwood的 Standup Timer 项目。本文将把研究的内容笔记整理,建立一个索引列表。

PowerManager.WakeLock

  PowerManager.WakerLock是我分析Standup Timer源代码时发现的一个小知识点,Standup Timer 用WakeLock保证程序运行时保持手机屏幕的恒亮(程序虽小但也做得相当的细心,考虑的很周到)。PowerManager 和PowerManager.WakerLock7用于对Android设备的电源进行管理。
  PowerManager:This class gives you control of the power state of the device.
  PowerManager.WakeLock: lets you say that you need to have the device on.
  Android中通过各种Lock锁对电源进行控制,需要注意的是加锁和解锁必须成对出现。先上一段Standup Timer里的代码然后进行说明。
private void acquireWakeLock() {
         if (wakeLock == null) {
                Logger.d("Acquiring wake lock");
                PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
                wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, this.getClass().getCanonicalName());
                wakeLock.acquire();
            }

    }

private void releaseWakeLock() {
        if (wakeLock != null && wakeLock.isHeld()) {
            wakeLock.release();
            wakeLock = null;
        }

    }

 

acquireWakeLock()方法中获取了 SCREEN_DIM_WAKE_LOCK锁,该锁使 CPU 保持运转,屏幕保持亮度(可以变灰)。这个函数在Activity的 onResume中被调用。releaseWakeLock()方法则是释放该锁。它在Activity的 onPause中被调用。利用Activiy的生命周期,巧妙的让 acquire()和release()成对出现。

    @Override
    protected void onResume()
    {
        super.onResume();
                //获取锁,保持屏幕亮度
        acquireWakeLock();
        startTimer();
    }
protected void onPause()
    {
        super.onPause();
         synchronized(this) {
                cancelTimer();
                releaseWakeLock();

                if (finished) {
                    clearState();
                } else {
                    saveState();
                }
            }
    }

 

PowerManager和WakeLock的操作步骤
  1.   PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);通过 Context.getSystemService().方法获取PowerManager实例。
  2.   然后通过PowerManager的newWakeLock((int flags, String tag)来生成WakeLock实例。int Flags指示要获取哪种WakeLock,不同的Lock对cpu 、屏幕、键盘灯有不同影响。
  3.   获取WakeLock实例后通过acquire()获取相应的锁,然后进行其他业务逻辑的操作,最后使用release()释放(释放是必须的)。

关于int flags

  各种锁的类型对CPU 、屏幕、键盘的影响:

PARTIAL_WAKE_LOCK:保持CPU 运转,屏幕和键盘灯有可能是关闭的。

SCREEN_DIM_WAKE_LOCK:保持CPU 运转,允许保持屏幕显示但有可能是灰的,允许关闭键盘灯

SCREEN_BRIGHT_WAKE_LOCK:保持CPU 运转,允许保持屏幕高亮显示,允许关闭键盘灯

FULL_WAKE_LOCK:保持CPU 运转,保持屏幕高亮显示,键盘灯也保持亮度

ACQUIRE_CAUSES_WAKEUP:Normal wake locks don’t actually turn on the illumination. Instead, they cause the illumination to remain on once it turns on (e.g. from user activity). This flag will force the screen and/or keyboard to turn on immediately, when the WakeLock is acquired. A typical use would be for notifications which are important for the user to see immediately.

ON_AFTER_RELEASE:f this flag is set, the user activity timer will be reset when the WakeLock is released, causing the illumination to remain on a bit longer. This can be used to reduce flicker if you are cycling between wake lock conditions.

权限获取

要进行电源的操作需要在AndroidManifest.xml中声明该应用有设置电源管理的权限。
<uses-permission android:name="android.permission.WAKE_LOCK" /> 你可能还需要 <uses-permission android:name="android.permission.DEVICE_POWER" />

另外WakeLock的设置是 Activiy 级别的,不是针对整个Application应用的。

 

摘自:http://fanwei51880.blog.163.com/blog/static/324067402010108105416798/

2011一月26

Android 开发:TextView 文字阴影效果

res/values/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AudioFileInfoOverlayText">
    <item name="android:paddingLeft">4px</item>
    <item name="android:paddingBottom">4px</item>
    <item name="android:textColor">#f1f1f1</item>
    <item name="android:textSize">60sp</item>
    <item name="android:shadowColor">#4d4d4d</item>
    <item name="android:shadowDx">0</item>
    <item name="android:shadowDy">-3</item>
    <item name="android:shadowRadius">3</item>
</style>
</resources>

res/layout/main.xml

<TextView style="@style/AudioFileInfoOverlayText" 
        android:id="@+id/title"
		android:layout_gravity="bottom"
		android:gravity="center_vertical|center_horizontal"
		android:layout_width="640px" android:layout_height="151px"
		android:background="@drawable/compass_bottom"
		></TextView>