Android custom message push and android custom message
Do not look at the figure:
After clicking, the effect is as follows:
Code: main method:
Package com. text. ac; import java. util. calendar; import android. app. activity; import android. app. alarmManager; import android. app. notification; import android. app. icationicationmanager; import android. app. pendingIntent; import android. content. context; import android. content. intent; import android. OS. bundle; import android. OS. systemClock; import android. view. view; import android. view. view. onClickListener; import android. widget. button;/***** @ author Hardi **/public class TextActivity extends Activity {Button button; Button buttonstop; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); button = (Button) findViewById (R. id. button); buttonstop = (Button) findViewById (R. id. titlebutton); button. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {Intent intent = new Intent (); // set the Action attribute intent. setAction ("com. text. ac. action. MY_SERVICE "); // start the Service startService (intent); // startService (new Intent (ExTextActivity. this, MessageService. class) ;}}); buttonstop. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {Intent intent = new Intent (); // set the Action attribute intent. setAction ("com. text. ac. action. MY_SERVICE "); // close this ServicestopService (intent );}});}}
Write a service:
Package com. text. ac; import android. app. notification; import android. app. icationicationmanager; import android. app. pendingIntent; import android. app. service; import android. content. context; import android. content. intent; import android. OS. IBinder; import android. widget. toast; public class MessageService extends Service {// gets the message thread private MessageThread messageThread = null; // click to view private Intent messageIntent = Null; private PendingIntent messagePendingIntent = null; // Notification bar message private int messageNotificationID = 1000; private Notification messageNotification = null; private icationicationmanager identifier = null; public IBinder onBind (Intent intent) {return null ;}@ Overridepublic void onCreate () {// initialize messageNotification = new Notification (); messageNotification. icon = R. drawable. ic_heh E; messageNotification. tickerText = "New message"; messageNotification. defaults = Notification. DEFAULT_SOUND; messageNotificatioManager = (icationicationmanager) getSystemService (Context. NOTIFICATION_SERVICE); // click the activity messageIntent = new Intent (this, TextActivity. class); messagePendingIntent = PendingIntent. getActivity (this, 0, messageIntent, 0); // enable the thread messageThread = new MessageThread (); messageThread. IsRunning = true; messageThread. start (); Toast. makeText (MessageService. this, "aaaa", Toast. LENGTH_LONG ). show (); super. onCreate ();}/*** get the message from the server **/class MessageThread extends Thread {// running status. In the next step, public boolean isRunning = true is used; public void run () {while (isRunning) {try {// take a rest for 10 minutes Thread. sleep (5000); // get the server message String serverMessage = getServerMessage (); if (serverMessage! = Null &&! "". Equals (serverMessage) {// update notification bar messageNotification. setLatestEventInfo (MessageService. this, "New message", "you won the prize, 5 million! "+ ServerMessage, messagePendingIntent); messageNotificatioManager. Y (messageNotificationID, messageNotification); // after each notification, the notification ID increments to avoid overwriting messageNotificationID ++;} catch (InterruptedException e) {e. printStackTrace () ;}}}@ Overridepublic void onDestroy () {// System. exit (0); // or, either. We recommend that you use System. exit (0). In this way, the process exits more cleanly. messageThread. isRunning = false; super. onDestroy ();}/*** here, this method is used as the server Demo. Only for example * @ return is used to return the message to be pushed by the server. Otherwise, if it is null, do not push */public String getServerMessage () {return "good ";}}
Click "run !! Demo download click
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.