WEB开发网
开发学院手机开发Android 开发 Android 串行接口(serial port)编程 阅读

Android 串行接口(serial port)编程

 2013-03-12 15:26:28 来源:开发学院   
核心提示:Preferences.javapackage com.whyonly.serial;import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Edit

Preferences.java

package com.whyonly.serial;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class Preferences {
	public static void saveUSBSetting(Context activity){
		SharedPreferences pref = getAppPrefs(activity);
		Editor editor = pref.edit();
		editor.putString("DEVICE", "/dev/ttyUSB0");
		editor.putString("BAUDRATE", "9600");
		editor.commit(); 	
	} 
	
	public static SharedPreferences getAppPrefs(Context context) {
		return ((SerialApplication)context.getApplicationContext()).getmPrefs();
	}
}

DialogUtil.java

package com.whyonly.serial;



import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

public class DialogUtil {
	
	public static void showWarningDialog(Context context,int messageid){
		showWarningDialog(context,context.getResources().getString(messageid));
	}
	
	public static void showWarningDialog(Context context,String message){
		String warningTitle = context.getResources().getString(R.string.defaultWarningTitle);
		String warningButtonName = context.getResources().getString(R.string.defaultWarningButton);
		showWarningDialog(context,warningTitle,message,warningButtonName);
	}
	
	private static void showWarningDialog(Context context,String title,String message,String positive){
		final AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(title).setMessage(message);
		builder.setPositiveButton(positive, new DialogInterface.OnClickListener() {  
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();  
			}	
		   });
		builder.create().show();
	}

}

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">serial</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="defaultWarningTitle">Taxi Driver</string>
	<string name="defaultWarningButton">Yes</string>
    <string name="error_configuration">Please configure your serial port first.</string>
    <string name="error_security">You do not have read/write permission to the serial
		port.</string>
    <string name="error_unknown">The serial port can not be opened for an unknown
		reason.</string>

</resources>

上一页  1 2 3 

Tags:Android 串行 接口

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接