Implement surfaceview dual-buffer mechanism by refreshing scheduled notifications

Source: Internet
Author: User

After reading some examples, we have made some simple things. I hope to provide relevant reference to relevant friends. I 'd like to paste the source code directly:

Main class:

Package com. JSD. Demo;

Import java. util. timer;
Import java. util. timertask;

Import Android. App. activity;
Import Android. App. notification;
Import Android. App. icationicationmanager;
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 icationicationmanager 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, pelateyou 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 ....");
}
}

 

Service class:

Package com. JSD. Demo. Service;

Import Android. App. notification;
Import Android. App. icationicationmanager;
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;
Icationicationmanager nm;
 
@ Override
Public ibinder onbind (intent ){
Return NULL;
}
 
@ Override
Public void onstart (intent, int startid ){
Nm = (icationicationmanager) getsystemservice (notification_service );
Shownotification (R. drawable. canshu_3, "Clear user! "," The CLEAR user! "," This is for clear current user that you operation to app, pelateyou 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. icationy (icationication_id, noti );
System. Out. println ("notification ....");
}
}

 

Surfaceview implementation class:

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 (); // automatically runs surfacecreated and surfacechanged
}
Class clickevent implements view. onclicklistener {
@ Override
Public void onclick (view v ){
If (V = btnsinglethread ){
New load_drawimage (0, 0). Start (); // open a thread to read and draw
} Else if (V = btndoublethread ){
New LoadImage (). Start (); // open a thread to read
New drawimage (imgwidth + 10, 0). Start (); // open a thread drawing
}
}
}
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 ");
// Use the reflection mechanism to obtain the image ID and size in the Resource
Field [] fields = R. drawable. Class. getdeclaredfields ();
For (field: fields ){
If (! "Icon". Equals (field. getname () // image except 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 ();
}
// Save the image ID
Imglist. Add (INDEX );
}
}
// Obtain the image size
Bitmap bmimg = bitmapfactory. decoderesource (getresources (),
Imglist. Get (0 ));
Imgwidth = bmimg. getwidth ();
Imgheight = bmimg. getheight ();
}
@ Override
Public void surfacedestroyed (surfaceholder holder ){
Log. I ("surface:", "Destroy ");
}
}
/**
* Threads for reading and displaying images
*/
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); // update the Screen Content
}
}
};

/**
* Only responsible for drawing threads
*/
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) {// if the image is valid
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); // update the Screen Content
}
}
}
};

/**
* Only threads responsible for reading Images
*/
Class LoadImage extends thread {
Int imgindex = 0;

Public void run (){
While (true ){
Bitmap = bitmapfactory. decoderesource (getresources (),
Imglist. Get (imgindex ));
Imgindex ++;
If (imgindex = imglist. Size () // re-read if the end is reached
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 = "single independent thread"> </button>
<Button Android: Id = "@ + ID/button02" Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" Android: text = "two independent Threads"> </button>
</Linearlayout>
<Surfaceview Android: Id = "@ + ID/surfaceview01"
Android: layout_width = "fill_parent" Android: layout_height = "fill_parent">
</Surfaceview>
</Linearlayout>

 

Noti. xml: Just a 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" type = "codeph" text = "/codeph">
<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, all the Code implemented above can be used directly after CP .....

Start the notification directly. You can open the notification after clicking the button and click its notification content. You can enter the next activ ity and then test the difference between the surfaceview single thread and double thread:

Figure:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.