定时通知(Notification)刷新实现SurfaceView双缓冲机制
看了些例子,也就简单些了些东西,希望能供给相关朋友做相关的参考,还是一样直接贴源码吧:
主类:
package com.jsd.demo;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.jsd.demo.service.ServiceDemo;
/**
*
* @author jiangshide
*
*/
public class DemoNoti extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.noti);
findViewById();
}
private int id = 1223171712;
private NotificationManager nm;
private void findViewById(){
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(listener);
Timer time = new Timer();
time.schedule(new TimerTask() {
@Override
public void run() {
startService(new Intent(DemoNoti.this,ServiceDemo.class));
System.out.println("run => server");
// showNotifaction(R.drawable.canshu_6,"clear user!","The Clear User!","This is for clear current user that you operation to app,pelease you relogin for it!");
}
}, 0);
}
private OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
startService(new Intent(DemoNoti.this,ServiceDemo.class));
System.out.println("onClick run => server");
}
};
private void showNotifaction(int icon,String tic,String title,String content){
Notification noti = new Notification(icon, tic,System.currentTimeMillis());
noti.defaults = Notification.DEFAULT_ALL;
PendingIntent intent = PendingIntent.getActivity(this, 0,new Intent(this,MainActivity.class),0);
noti.setLatestEventInfo(this, title, content, intent);
nm.notify(id, noti);
System.out.println("notification....");
}
}
服务类:
package com.jsd.demo.service;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import com.jsd.demo.MainActivity;
import com.jsd.demo.R;
/**
*
* @author jiangshide
*
*/
public class ServiceDemo extends Service{
int notification_id=19172439;
NotificationManager nm;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onStart(Intent intent, int startId) {
nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
showNotification(R.drawable.canshu_3,"clear user!","The Clear User!","This is for clear current user that you operation to app,pelease you relogin for it!");
}
private void showNotification(int icon,String tic,String title,String content){
Notification noti = new Notification(icon, tic,System.currentTimeMillis());
noti.defaults = Notification.DEFAULT_ALL;
PendingIntent intent = PendingIntent.getActivity(this, 0,new Intent(this,MainActivity.class),0);
noti.setLatestEventInfo(this, title, content, intent);
nm.notify(notification_id, noti);
System.out.println("notification....");
}
}
SurfaceView实现类:
package com.jsd.demo;
import java.lang.reflect.Field;
import java.util.ArrayList;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
/**
*
* @author jiangshide
*
*/
public class MainActivity extends Activity {
Button btnSingleThread, btnDoubleThread;
SurfaceView sfv;
SurfaceHolder sfh;
ArrayList<Integer> imgList = new ArrayList<Integer>();
int imgWidth, imgHeight;
Bitmap bitmap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById();
}
private void findViewById() {
btnSingleThread = (Button) this.findViewById(R.id.Button01);
btnDoubleThread = (Button) this.findViewById(R.id.Button02);
btnSingleThread.setOnClickListener(new ClickEvent());
btnDoubleThread.setOnClickListener(new ClickEvent());
sfv = (SurfaceView) this.findViewById(R.id.SurfaceView01);
sfh = sfv.getHolder();
sfh.addCallback(new MyCallBack());// 自动运行surfaceCreated以及surfaceChanged
}
class ClickEvent implements View.OnClickListener {
@Override
public void onClick(View v) {
if (v == btnSingleThread) {
new Load_DrawImage(0, 0).start();//开一条线程读取并绘图
} else if (v == btnDoubleThread) {
new LoadImage().start();//开一条线程读取
new DrawImage(imgWidth + 10, 0).start();//开一条线程绘图
}
}
}
class MyCallBack implements SurfaceHolder.Callback {
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
Log.i("Surface:", "Change");
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.i("Surface:", "Create");
// 用反射机制来获取资源中的图片ID和尺寸
Field[] fields = R.drawable.class.getDeclaredFields();
for (Field field : fields) {
if (!"icon".equals(field.getName()))// 除了icon之外的图片
{
int index = 0;
try {
index = field.getInt(R.drawable.class);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 保存图片ID
imgList.add(index);
}
}
// 取得图像大小
Bitmap bmImg = BitmapFactory.decodeResource(getResources(),
imgList.get(0));
imgWidth = bmImg.getWidth();
imgHeight = bmImg.getHeight();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.i("Surface:", "Destroy");
}
}
/**
* 读取并显示图片的线程
*/
class Load_DrawImage extends Thread {
int x, y;
int imgIndex = 0;
public Load_DrawImage(int x, int y) {
this.x = x;
this.y = y;
}
public void run() {
while (true) {
Canvas c = sfh.lockCanvas(new Rect(this.x, this.y, this.x
+ imgWidth, this.y + imgHeight));
Bitmap bmImg = BitmapFactory.decodeResource(getResources(),
imgList.get(imgIndex));
c.drawBitmap(bmImg, this.x, this.y, new Paint());
imgIndex++;
if (imgIndex == imgList.size())
imgIndex = 0;
sfh.unlockCanvasAndPost(c);// 更新屏幕显示内容
}
}
};
/**
* 只负责绘图的线程
*/
class DrawImage extends Thread {
int x, y;
public DrawImage(int x, int y) {
this.x = x;
this.y = y;
}
public void run() {
while (true) {
if (bitmap != null) {//如果图像有效
Canvas c = sfh.lockCanvas(new Rect(this.x, this.y, this.x
+ imgWidth, this.y + imgHeight));
c.drawBitmap(bitmap, this.x, this.y, new Paint());
sfh.unlockCanvasAndPost(c);// 更新屏幕显示内容
}
}
}
};
/**
* 只负责读取图片的线程
*/
class LoadImage extends Thread {
int imgIndex = 0;
public void run() {
while (true) {
bitmap = BitmapFactory.decodeResource(getResources(),
imgList.get(imgIndex));
imgIndex++;
if (imgIndex == imgList.size())//如果到尽头则重新读取
imgIndex = 0;
}
}
};
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout android:id="@+id/LinearLayout1"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button android:id="@+id/Button01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="单个独立线程"></Button>
<Button android:id="@+id/Button02" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="两个独立线程"></Button>
</LinearLayout>
<SurfaceView android:id="@+id/SurfaceView01"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</SurfaceView>
</LinearLayout>
noti.xml: 就一个BUTTON
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="notification"
/>
</LinearLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jsd.demo"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
</activity>
<activity android:name=".DemoNoti">
</activity>
<activity android:name=".DemoNoti">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:enabled="true" android:name=".service.ServiceDemo"/>
</application>
<uses-permission android:name="android.permission.VIBRATE" />
</manifest>
OK,以上就实现的全部代码,可以直接CP后使用.....
直接启动通知,与点击按钮通知后打开通知点击其通知内容你可以进入下个ACTIV ITY然后测试SURFACEVIEW单线程与双线程所存在的区别:
看图: