当前位置:  编程技术>移动开发
本页文章导读:
    ▪画图写字范例        画图写字实例 public class DrawDemo extends Activity { DemoView demoview; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); demoview = new De.........
    ▪ inputstream ,outputstream,AssetManager asset的应用        inputstream ,outputstream,AssetManager asset的使用 import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import android.app.Activity; import android.content.res.AssetManager; import android.os.Bundle; impor.........
    ▪ 监听短信并判断是不是未读       监听短信并判断是否未读 final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED"; BroadcastReceiver SMSbr = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) .........

[1]画图写字范例
    来源: 互联网  发布时间: 2014-02-18
画图写字实例
public class DrawDemo extends Activity {
	DemoView demoview;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		demoview = new DemoView(this);
		setContentView(demoview);
	}

	private class DemoView extends View{
		public DemoView(Context context){
			super(context);
		}

		@Override protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);

			// custom drawing code here
			// remember: y increases from top to bottom
			// x increases from left to right
			int x = 0;
			int y = 0;
			Paint paint = new Paint();
			paint.setStyle(Paint.Style.FILL);

			// make the entire canvas white
			paint.setColor(Color.WHITE);
			canvas.drawPaint(paint);
			// another way to do this is to use:
			// canvas.drawColor(Color.WHITE);

			// draw a solid blue circle
			paint.setColor(Color.BLUE);
			canvas.drawCircle(20, 20, 15, paint);

			// draw blue circle with antialiasing turned on
			paint.setAntiAlias(true);
			paint.setColor(Color.BLUE);
			canvas.drawCircle(60, 20, 15, paint);

			// compare the above circles once drawn
			// the fist circle has a jagged perimeter
			// the second circle has a smooth perimeter

			// draw a solid red rectangle
			paint.setAntiAlias(false);
			paint.setColor(Color.RED);

			// create and draw triangles
			// use a Path object to store the 3 line segments
			// use .offset to draw in many locations
			// note: this triangle is not centered at 0,0
			paint.setStyle(Paint.Style.STROKE);
			paint.setStrokeWidth(2);
			paint.setColor(Color.RED);
			Path path = new Path();
			path.moveTo(0, -10);
			path.lineTo(5, 0);
			path.lineTo(-5, 0);
			path.close();
			path.offset(10, 40);
			canvas.drawPath(path, paint);
			path.offset(50, 100);
			canvas.drawPath(path, paint);
			// offset is cumlative
			// next draw displaces 50,100 from previous
			path.offset(50, 100);
			canvas.drawPath(path, paint);

			// draw some text using STROKE style
			paint.setStyle(Paint.Style.STROKE);
			paint.setStrokeWidth(1);
			paint.setColor(Color.MAGENTA);
			paint.setTextSize(30);
			canvas.drawText("Style.STROKE", 75, 75, paint);

			// draw some text using FILL style
			paint.setStyle(Paint.Style.FILL);
			//turn antialiasing on
			paint.setAntiAlias(true);
			paint.setTextSize(30);
			canvas.drawText("Style.FILL", 75, 110, paint);

			// draw some rotated text
			// get text width and height
			// set desired drawing location
			x = 75;
			y = 185;
			paint.setColor(Color.GRAY);
			paint.setTextSize(25);
			String str2rotate = "Rotated!";

			// draw bounding rect before rotating text
			Rect rect = new Rect();
			paint.getTextBounds(str2rotate, 0, str2rotate.length(), rect);
			canvas.translate(x, y);
			paint.setStyle(Paint.Style.FILL);
			// draw unrotated text
			canvas.drawText("!Rotated", 0, 0, paint);
			paint.setStyle(Paint.Style.STROKE);
			canvas.drawRect(rect, paint);
			// undo the translate
			canvas.translate(-x, -y);

			// rotate the canvas on center of the text to draw
			canvas.rotate(-45, x + rect.exactCenterX(),
                                               y + rect.exactCenterY());
			// draw the rotated text
			paint.setStyle(Paint.Style.FILL);
			canvas.drawText(str2rotate, x, y, paint);

			//undo the rotate
			canvas.restore();
			canvas.drawText("After canvas.restore()", 50, 250, paint);

			// draw a thick dashed line
			DashPathEffect dashPath =
                            new DashPathEffect(new float[]{20,5}, 1);
			paint.setPathEffect(dashPath);
			paint.setStrokeWidth(8);
			canvas.drawLine(0, 300 , 320, 300, paint);

		}
	}
}

    
[2] inputstream ,outputstream,AssetManager asset的应用
    来源: 互联网  发布时间: 2014-02-18
inputstream ,outputstream,AssetManager asset的使用
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;

/**
 * Class which shows how to use assets
 *
 * @author FaYnaSoft Labs
 */
public class Main extends Activity {
	private EditText firstField;
	private EditText secondField;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		AssetManager assetManager = getAssets();
		String[] files = null;
		try {
			files = assetManager.list("image");
		} catch (IOException e) {
			Log.e("tag", e.getMessage());
		}
		firstField = (EditText) findViewById(R.id.firstId);
		firstField.setText(Integer.toString(files.length) + " file. File name is "
				+ files[0]);
		InputStream inputStream = null;
		try {
			inputStream = assetManager.open("readme.txt");
		} catch (IOException e) {
			Log.e("tag", e.getMessage());
		}

		String s = readTextFile(inputStream);
		secondField = (EditText) findViewById(R.id.secondId);
		secondField.setText(s);
	}

	/**
	 * This method reads simple text file
	 * @param inputStream
	 * @return data from file
	 */
	private String readTextFile(InputStream inputStream) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		byte buf[] = new byte[1024];
		int len;
		try {
			while ((len = inputStream.read(buf)) != -1) {
				outputStream.write(buf, 0, len);
			}
			outputStream.close();
			inputStream.close();
		} catch (IOException e) {
		}
		return outputStream.toString();
	}
}


    
[3] 监听短信并判断是不是未读
    来源: 互联网  发布时间: 2014-02-18
监听短信并判断是否未读
final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";   
BroadcastReceiver SMSbr = new BroadcastReceiver() {   
    
        @Override  
        public void onReceive(Context context, Intent intent) {   
                              Bundle bundle = intent.getExtras();   
                if (bundle != null) {   
                                                Object[] pdus = (Object[]) bundle.get("pdus");   
                        final SmsMessage[] messages = new SmsMessage[pdus.length];   
                        for (int i = 0; i < pdus.length; i++)   
                                messages[i] = SmsMessage   
                                                .createFromPdu((byte[]) pdus[i]);   
                        if (messages.length > -1) {   
                              .   
                                String smsToast = "New SMS received from "  
                                                + messages[0].getOriginatingAddress() + "\n'"  
                                                + messages[0].getMessageBody() + "'";   
                                Toast.makeText(context, smsToast, Toast.LENGTH_LONG)   
                                                .show();   
                        }   
                }   
        }   
};   
  
IntentFilter SMSfilter = new IntentFilter(SMS_RECEIVED);   
this.registerReceiver(SMSbr, SMSfilter);  

final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
BroadcastReceiver SMSbr = new BroadcastReceiver() {
 
        @Override
        public void onReceive(Context context, Intent intent) {
                              Bundle bundle = intent.getExtras();
                if (bundle != null) {
                                                Object[] pdus = (Object[]) bundle.get("pdus");
                        final SmsMessage[] messages = new SmsMessage[pdus.length];
                        for (int i = 0; i < pdus.length; i++)
                                messages[i] = SmsMessage
                                                .createFromPdu((byte[]) pdus[i]);
                        if (messages.length > -1) {
                              .
                                String smsToast = "New SMS received from "
                                                + messages[0].getOriginatingAddress() + "\n'"
                                                + messages[0].getMessageBody() + "'";
                                Toast.makeText(context, smsToast, Toast.LENGTH_LONG)
                                                .show();
                        }
                }
        }
};

IntentFilter SMSfilter = new IntentFilter(SMS_RECEIVED);
this.registerReceiver(SMSbr, SMSfilter); 

 
private boolean checkSMS() {   
        // Sets the sms inbox's URI   
        Uri uriSMS = Uri.parse("content://sms");   
        Cursor c = getBaseContext().getContentResolver().query(uriSMS, null,   
                        "read = 0", null, null);   
        // Checks the number of unread messages in the inbox   
        if (c.getCount() == 0) {   
                return false;   
        } else  
                return true;   
}  

private boolean checkSMS() {
        // Sets the sms inbox's URI
        Uri uriSMS = Uri.parse("content://sms");
        Cursor c = getBaseContext().getContentResolver().query(uriSMS, null,
                        "read = 0", null, null);
        // Checks the number of unread messages in the inbox
        if (c.getCount() == 0) {
                return false;
        } else
                return true;
} 

 
<uses-permission id="android.permission.RECEIVE_SMS" />   
<uses-permission id="android.permission.READ_SMS" />  


    
最新技术文章:
▪Android开发之登录验证实例教程
▪Android开发之注册登录方法示例
▪Android获取手机SIM卡运营商信息的方法
▪Android实现将已发送的短信写入短信数据库的...
▪Android发送短信功能代码
▪Android根据电话号码获得联系人头像实例代码
▪Android中GPS定位的用法实例
▪Android实现退出时关闭所有Activity的方法
▪Android实现文件的分割和组装
▪Android录音应用实例教程
▪Android双击返回键退出程序的实现方法
▪Android实现侦听电池状态显示、电量及充电动...
▪Android获取当前已连接的wifi信号强度的方法
▪Android实现动态显示或隐藏密码输入框的内容
▪根据USER-AGENT判断手机类型并跳转到相应的app...
▪Android Touch事件分发过程详解
▪Android中实现为TextView添加多个可点击的文本
▪Android程序设计之AIDL实例详解
▪Android显式启动与隐式启动Activity的区别介绍
▪Android按钮单击事件的四种常用写法总结
▪Android消息处理机制Looper和Handler详解
▪Android实现Back功能代码片段总结
▪Android实用的代码片段 常用代码总结
▪Android实现弹出键盘的方法
▪Android中通过view方式获取当前Activity的屏幕截...
▪Android提高之自定义Menu(TabMenu)实现方法
▪Android提高之多方向抽屉实现方法
▪Android提高之MediaPlayer播放网络音频的实现方法...
▪Android提高之MediaPlayer播放网络视频的实现方法...
▪Android提高之手游转电视游戏的模拟操控
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3