很多时候,需要在一些锁屏,动态墙纸,启动器中加入提示信息,比如时间,日期,短信,电话,电池等信息,封装成一个全屏的VIEW类,既可以当控件在静态布局中使用,也可以在动态布局中使用addView加入,十分方便。支持文字和图片两种方式显示。
package com.example.scr;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.Paint.FontMetrics;
import android.graphics.Paint.Style;
import android.net.Uri;
import android.os.Handler;
import android.provider.CallLog.Calls;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
class PromptMessageView extends View {
private static final boolean isImg = false;
private int scr_w = 0, scr_h = 0;
Context mContext = null;
ArrayList BmWeek = new ArrayList(12);
ArrayList BmTime = new ArrayList(11);
ArrayList BmDate = new ArrayList(8);
protected String mcallstr = "未接电话:";
protected String msmsstr = "未读短信:";
protected int m_callcount = 0;// findMissedCallCount();
protected int m_smscount = 0;// findNewSmsCount() + findNewMmsCount();
// 圆心g_x, g_y,半径g_r, 手指位移m_lx, m_ly
public int g_r = 0;
public void moveView(int gr) {
g_r = gr;
invalidate();
}
public PromptMessageView(Context context) {
super(context);
mContext = context;
m_callcount = findMissedCallCount();
m_smscount = findNewSmsCount() + findNewMmsCount();
if (isImg) {
decodeDate();
decodeTime();
decodeWeek();
}
context.getApplicationContext()
.getContentResolver()
.registerContentObserver(
Uri.parse("content://mms-sms/"),
true,
new newMmsContentObserver(context
.getApplicationContext(), new Handler()));
context.getApplicationContext()
.getContentResolver()
.registerContentObserver(
/* content://call_log/calls */
android.provider.CallLog.Calls.CONTENT_URI,
true,
new MissedCallContentObserver(context
.getApplicationContext(), new Handler()));
}
public PromptMessageView(Context context, int w, int h) {
super(context);
scr_w = w;
scr_h = h;
mContext = context;
m_callcount = findMissedCallCount();
m_smscount = findNewSmsCount() + findNewMmsCount();
if (isImg) {
decodeDate();
decodeTime();
decodeWeek();
}
context.getApplicationContext()
.getContentResolver()
.registerContentObserver(
Uri.parse("content://mms-sms/"),
true,
new newMmsContentObserver(context
.getApplicationContext(), new Handler()));
context.getApplicationContext()
.getContentResolver()
.registerContentObserver(
/* content://call_log/calls */
android.provider.CallLog.Calls.CONTENT_URI,
true,
new MissedCallContentObserver(context
.getApplicationContext(), new Handler()));
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
if (!isImg) {
// 设置字体
Paint p = new Paint();
p.setColor(Color.WHITE);
p.setStrokeWidth(1);
p.setStyle(Style.FILL);
p.setTextSize(26);
Typeface font = Typeface.create("黑体", Typeface.BOLD);
p.setTypeface(font);
// 显示时间
boolean isFontMid = false; // 是否居中显示
float str_left = 10.0f;
p.setTextSize(120);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String time = sdf.format(new Date());
time = time.substring(0, time.length() - 3);
int fw = (int) p.measureText(time);
FontMetrics fm = p.getFontMetrics();
int fh = (int) Math.ceil(fm.descent - fm.top) + 2;
if (isFontMid) {
canvas.drawText(time, (scr_w - fw) / 2, 200 - g_r, p);
} else {
canvas.drawText(time, str_left, 200 - g_r, p);
}
// 显示星期
sdf = new SimpleDateFormat("EEEE");
String week = sdf.format(new Date());
// 显示日期+星期
p.setTextSize(20);
sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(new Date());
date = date.substring(5, date.length());
date = date + "," + week;
fw = (int) p.measureText(date);
if (isFontMid) {
canvas.drawText(date, (scr_w - fw) / 2, 200 + 30 - g_r, p);
} else {
canvas.drawText(date, str_left, 200 + 30 - g_r, p);
}
// 显示未接电话
p.setTextSize(15);
if (isFontMid) {
canvas.drawText(mcallstr + String.valueOf(m_callcount),
(scr_w - fw) / 2, 300 - g_r, p);
} else {
canvas.drawText(mcallstr + String.valueOf(m_callcount),
str_left, 300 - g_r, p);
}
// 显示未读短信
if (isFontMid) {
canvas.drawText(msmsstr + String.valueOf(m_smscount),
(scr_w - fw) / 2, 350 - g_r, p);
} else {
canvas.drawText(msmsstr + String.valueOf(m_smscount), str_left,
320 - g_r, p);
}
} else {
// 贴图实现
// drawDate(tmpCanvas, 100, 500);
// drawTime(tmpCanvas, 100, 400);
// drawWeek(tmpCanvas, 300, 500);
newDrawDate(canvas, 100, 500 - g_r);
newDrawTime(canvas, 100, 400 - g_r);
newDrawWeek(canvas, 300, 500 - g_r);
}
}
// 监控电话数目
private int findMissedCallCount() {
int missedCallCount = 0;
StringBuilder where = new StringBuilder("type = ");
where.append(Calls.MISSED_TYPE);
where.append(" AND new = 1");
// start the query
Cursor cur = null;
try {
cur = mContext
.getApplicationContext()
.getContentResolver()
.query(Calls.CONTENT_URI, new String[] { Calls._ID },
where.toString(), null, Calls.DEFAULT_SORT_ORDER);
if (cur != null) {
missedCallCount = cur.getCount();
}
} catch (Exception ex) {
} finally {
if (cur != null) {
cur.close();
}
}
return missedCallCount;
}
// 监控短信,彩信数目变化
private int findNewSmsCount() {
Cursor csr = null;
int newSmsCount = 0;
try {
csr = mContext
.getApplicationContext()
.getContentResolver()
.query(Uri.parse("content://sms"), null,
"type = 1 and read = 0", null, null);
newSmsCount = csr.getCount(); // 未读短信数目
} catch (Exception e) {
e.printStackTrace();
} finally {
if (csr != null)
csr.close();
}
return newSmsCount;
}
// 监控短信,短信数目变化
private int findNewMmsCount() {
Cursor csr = null;
int newMmsCount = 0;
try {
csr = mContext
.getApplicationContext()
.getContentResolver()
.query(Uri.parse("content://mms/inbox"), null, "read = 0",
null, null);
newMmsCount = csr.getCount();// 未读彩信数目
} catch (Exception e) {
e.printStackTrace();
} finally {
if (csr != null)
csr.close();
}
return newMmsCount;
}
// 监控信息数据库
public class newMmsContentObserver extends ContentObserver {
private Context ctx;
private Handler m_handler;
int newMmsCount = 0;
int newSmsCount = 0;
public newMmsContentObserver(Context context, Handler handler) {
super(handler);
ctx = context;
m_handler = handler;
}
@Override
public void onChange(boolean selfChange) {
newMmsCount = findNewSmsCount();
newSmsCount = findNewMmsCount();
Log.i("@@@@@", "newSmsCount = " + (newSmsCount + newMmsCount));
m_smscount = newMmsCount + newSmsCount;
postInvalidate();
}
}
// 监控电话数据库
public class MissedCallContentObserver extends ContentObserver {
private Context ctx;
int missedCallCount = 0;
private Handler m_handler;
private static final String TAG = "MissedCallContentObserver";
public MissedCallContentObserver(Context context, Handler handler) {
super(handler);
ctx = context;
m_handler = handler;
}
@Override
public void onChange(boolean selfChange) {
missedCallCount = findMissedCallCount();
Log.i("@@@@@", "missedCallCount = " + missedCallCount);
m_callcount = missedCallCount;
postInvalidate();
}
}
public void decodeDate() {
Bitmap bm = null;
int[] imgIds = { R.drawable.r011, R.drawable.r111, R.drawable.r211,
R.drawable.r311, R.drawable.r411, R.drawable.r511,
R.drawable.r611, R.drawable.r711, R.drawable.r811,
R.drawable.r911, R.drawable.hy11, R.drawable.h711, };
for (int i = 0; i < imgIds.length; i++) {
bm = BitmapFactory.decodeResource(mContext.getResources(),
imgIds[i]);
BmDate.add(bm);
// BmDate[i] = bm;
}
}
public void newDrawDate(Canvas canvas, int x, int y) {
Bitmap bm = null;
int pos = x;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(new Date());
date = date.substring(5, date.length());
// canvas.drawText(date, x, y, null);
for (int i = 0; i < date.length(); i++) {
if (date.charAt(i) == '-') {
bm = BmDate.get(10);
// bm = BmDate[10];
} else {
bm = BmDate.get(date.charAt(i) - '0');
// bm = BmDate[date.charAt(i) - '0'];
}
canvas.drawBitmap(bm, pos, y, null);
pos += bm.getWidth();
}
bm = BmDate.get(11);
// bm = BmDate[11];
canvas.drawBitmap(bm, pos, y, null);
}
public void drawDate(Canvas canvas, int x, int y) {
Bitmap bm = null;
int[] imgIds = { R.drawable.r011, R.drawable.r111, R.drawable.r211,
R.drawable.r311, R.drawable.r411, R.drawable.r511,
R.drawable.r611, R.drawable.r711, R.drawable.r811,
R.drawable.r911, R.drawable.hy11, R.drawable.h711, };
int pos = x;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(new Date());
date = date.substring(5, date.length());
// canvas.drawText(date, x, y, null);
for (int i = 0; i < date.length(); i++) {
if (date.charAt(i) == '-') {
bm = BitmapFactory.decodeResource(mContext.getResources(),
imgIds[10]);
} else {
bm = BitmapFactory.decodeResource(mContext.getResources(),
imgIds[date.charAt(i) - '0']);
}
canvas.drawBitmap(bm, pos, y, null);
pos += bm.getWidth();
}
bm = BitmapFactory.decodeResource(mContext.getResources(), imgIds[11]);
canvas.drawBitmap(bm, pos, y, null);
}
public void decodeTime() {
Bitmap bm = null;
int[] imgIds = { R.drawable.time_0, R.drawable.time_1,
R.drawable.time_2, R.drawable.time_3, R.drawable.time_4,
R.drawable.time_5, R.drawable.time_6, R.drawable.time_7,
R.drawable.time_8, R.drawable.time_9, R.drawable.time_dot };
for (int i = 0; i < imgIds.length; i++) {
bm = BitmapFactory.decodeResource(mContext.getResources(),
imgIds[i]);
BmTime.add(bm);
}
}
public void newDrawTime(Canvas canvas, int x, int y) {
Bitmap bm = null;
int pos = x;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(new Date());
date = date.substring(5, date.length());
// canvas.drawText(date, x, y, null);
for (int i = 0; i < date.length(); i++) {
if (date.charAt(i) == '-') {
bm = BmTime.get(10);
} else {
bm = BmTime.get(date.charAt(i) - '0');
}
canvas.drawBitmap(bm, pos, y, null);
pos += bm.getWidth();
}
}
public void drawTime(Canvas canvas, int x, int y) {
Bitmap bm = null;
int[] imgIds = { R.drawable.time_0, R.drawable.time_1,
R.drawable.time_2, R.drawable.time_3, R.drawable.time_4,
R.drawable.time_5, R.drawable.time_6, R.drawable.time_7,
R.drawable.time_8, R.drawable.time_9, R.drawable.time_dot };
int pos = x;
SimpleDateFormat sdf = new SimpleDateFormat("HH-mm-ss");
String time = sdf.format(new Date());
time = time.substring(0, time.length() - 3);
// canvas.drawText(time, x, y, null);
for (int i = 0; i < time.length(); i++) {
if (time.charAt(i) == '-') {
bm = BitmapFactory.decodeResource(mContext.getResources(),
imgIds[10]);
} else {
bm = BitmapFactory.decodeResource(mContext.getResources(),
imgIds[time.charAt(i) - '0']);
}
canvas.drawBitmap(bm, pos, y, null);
pos += bm.getWidth();
}
}
public void decodeWeek() {
Bitmap bm = null;
int[] imgIds = { R.drawable.h111, R.drawable.h211, R.drawable.h311,
R.drawable.h411, R.drawable.h511, R.drawable.h611,
R.drawable.h711, R.drawable.hxq11 };
for (int i = 0; i < imgIds.length; i++) {
bm = BitmapFactory.decodeResource(mContext.getResources(),
imgIds[i]);
BmWeek.add(bm);
}
}
public void newDrawWeek(Canvas canvas, int x, int y) {
Bitmap bm = null;
String[] weeks = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };
int pos = x;
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
String week = sdf.format(new Date());
// canvas.drawText(week, x, y, null);
bm = BmWeek.get(7);
canvas.drawBitmap(bm, pos, y, null);
pos += bm.getWidth();
for (int i = 0; i < weeks.length; i++) {
if (weeks[i].equals(week)) {
bm = BmWeek.get(i);
canvas.drawBitmap(bm, pos, y, null);
break;
}
}
}
public void drawWeek(Canvas canvas, int x, int y) {
Bitmap bm = null;
String[] weeks = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };
int[] imgIds = { R.drawable.h111, R.drawable.h211, R.drawable.h311,
R.drawable.h411, R.drawable.h511, R.drawable.h611,
R.drawable.h711, R.drawable.hxq11 };
int pos = x;
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
String week = sdf.format(new Date());
// canvas.drawText(week, x, y, null);
bm = BitmapFactory.decodeResource(mContext.getResources(),
R.drawable.hxq11);
canvas.drawBitmap(bm, pos, y, null);
pos += bm.getWidth();
for (int i = 0; i < weeks.length; i++) {
if (weeks[i].equals(week)) {
bm = BitmapFactory.decodeResource(mContext.getResources(),
imgIds[i]);
canvas.drawBitmap(bm, pos, y, null);
break;
}
}
}
public void onViewDestroy() {
// TODO Auto-generated method stub
mContext.getContentResolver().unregisterContentObserver(
new newMmsContentObserver(mContext.getApplicationContext(),
new Handler()));
mContext.getContentResolver().unregisterContentObserver(
new MissedCallContentObserver(mContext.getApplicationContext(),
new Handler()));
}
}