Android high imitation UC browser and 360 mobile guard message resident bar (notification bar), androiduc
I read the custom message bar and notification bar on the Internet.The Notification control found that the UC browser is of this type. Today, I wrote a demo implementation,
Each button has different functions. The Code is as follows:
Package com. example. textwsjdemo; import android. app. activity; import android. app. notification; import android. app. icationicationmanager; import android. app. pendingIntent; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. content. intentFilter; import android. OS. bundle; import android. view. keyEvent; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. remoteViews; import android. widget. toast; public class MainActivity extends Activity {private Button bt_hehe; private icationicationmanager icationmanager; private Notification notification; private int icon; private CharSequence tickerText; private long when; RemoteViews contentView; private Intent intent; private PendingIntent pendingIntent; private int icationication_id = 0; private MyBroadCast receiver; private static String ACTION = "a"; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); consumer ER = new MyBroadCast (); IntentFilter filter = new IntentFilter (); filter. addAction ("a"); filter. addAction ("B"); filter. addAction ("c"); filter. addAction ("d"); registerReceiver (receiver, filter); initView (); initData ();} private void initData () {icon = R. drawable. ic_launcher; // notification icon tickerText = "Hello"; // notification text displayed in the status bar prompt when = System. currentTimeMillis (); // the time when a notification is generated. The private void initView () {bt_hehe = (Button) findViewById (R. id. bt_hehe); bt_hehe.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stub // start the prompt bar createNotification () ;}});} private void createNotification () {icationicationmanager = (icationicationmanager) getSystemService (Context. NOTIFICATION_SERVICE); notification = new Notification (); notification. icon = icon; notification. tickerText = tickerText; notification. when = when;/*** here we use a custom view to display Notification */contentView = new RemoteViews (getPackageName (), R. layout. notification_item); contentView. setTextViewText (R. id. text11, "novel"); contentView. setTextViewText (R. id. text22, "video"); contentView. setTextViewText (R. id. text33, "news"); contentView. setTextViewText (R. id. text44, "nonsense"); // contentView. setTextViewText (R. id. icationicationpercent, "0%"); // contentView. setProgressBar (R. id. notificationss, 100, 0, false); // progress bar // contentView. setImageViewResource (R. id. image, R. drawable. more_advice); // load the image // contentView. setImageViewResource (R. id. image, R. drawable. more_attention); // contentView. setImageViewResource (R. id. image, R. drawable. more_evaluate); // contentView. setImageViewResource (R. id. image, R. drawable. more_about); // contentView. setTextViewText (R. id. text, "Hello, this message is in a custom expanded view"); // text notification. flags = Notification. FLAG_ONGOING_EVENT; // set resident. Do not slide to cancel the event. // The default jump to the main interface intent = new Intent (this, MainActivity. class); intent. addFlags (Intent. FLAG_ACTIVITY_SINGLE_TOP); pendingIntent = PendingIntent. getActivity (this, 0, intent, 0); // custom jump contentView. setOnClickPendingIntent (R. id. ll_11, PendingIntent. getBroadcast (MainActivity. this, 11, new Intent (). setAction ("a"), PendingIntent. FLAG_UPDATE_CURRENT); contentView. setOnClickPendingIntent (R. id. ll_22, PendingIntent. getBroadcast (MainActivity. this, 11, new Intent (). setAction ("B"), PendingIntent. FLAG_UPDATE_CURRENT); contentView. setOnClickPendingIntent (R. id. ll_33, PendingIntent. getBroadcast (MainActivity. this, 11, new Intent (). setAction ("c"), PendingIntent. FLAG_UPDATE_CURRENT); contentView. setOnClickPendingIntent (R. id. ll_44, PendingIntent. getBroadcast (MainActivity. this, 11, new Intent (). setAction ("d"), PendingIntent. FLAG_UPDATE_CURRENT); notification. contentView = contentView; notification. contentIntent = pendingIntent; icationicationmanager. Y (ication_id _id, notification);} // cancel the private void cancelNotification () {icationicationmanager. cancelAll () ;}@ Overrideprotected void onDestroy () {cancelNotification (); unregisterReceiver (receiver) ;}@ Overridepublic boolean onKeyDown (int keyCode, KeyEvent event) {if (keyCode = KeyEvent. KEYCODE_BACK) {System. out. println ("the back key onKeyDown ()"); cancelNotification ();} return super. onKeyDown (keyCode, event);} class MyBroadCast extends BroadcastReceiver {@ Overridepublic void onReceive (Context context, Intent intent) {if (intent. getAction (). equals ("a") {Toast. makeText (MainActivity. this, "11111111111111", Toast. LENGTH_LONG ). show (); startActivity (new Intent (MainActivity. this, ActivityText1.class);} if (intent. getAction (). equals ("B") {Toast. makeText (MainActivity. this, "222222222222222", Toast. LENGTH_LONG ). show (); startActivity (new Intent (MainActivity. this, ActivityText2.class);} if (intent. getAction (). equals ("c") {Toast. makeText (MainActivity. this, "333333333333", Toast. LENGTH_LONG ). show (); startActivity (new Intent (MainActivity. this, ActivityText3.class);} if (intent. getAction (). equals ("d") {Toast. makeText (MainActivity. this, "4444444444444", Toast. LENGTH_LONG ). show (); startActivity (new Intent (MainActivity. this, ActivityText4.class ));}}}}
The following are some attribute settings:
/** Add sound * notification. defaults | = Notification. DEFAULT_SOUND; * or use the following methods * notification. sound = Uri. parse ("file: // sdcard/notification/ringer.pdf"); * notification. sound = Uri. withAppendedPath (Audio. media. INTERNAL_CONTENT_URI, "6"); * if you want to repeat the sound until the user responds to the notification, you can add "FLAG_INSISTENT" * to the flags field of notification. If the defaults field of notification includes the "DEFAULT_SOUND" attribute, this attribute will overwrite the sound defined in the sound field * // ** add vibration * notification. defaults | = Notification. DEFAULT_VIBRATE; * or you can define your own vibration mode: * long [] vibrate = {0,100,200,300}; // The vibration starts after 0 milliseconds and stops after 100 milliseconds of vibration, 200 ms later * notification. vibrate = vibrate; * The long array can be defined as any desired length * If the defaults field of notification includes "DEFAULT_VIBRATE ", this attribute will overwrite the vibration defined in the vibrate field * // ** Add LED light notification * notification. defaults | = Notification. DEFAULT_LIGHTS; * or use your own LED reminder mode: * notification. ledARGB = 0xff00ff00; * notification. ledOnMS = 300; // bright time * notification. ledOffMS = 1000; // destroy time * notification. flags | = Notification. FLAG_SHOW_LIGHTS; * // ** more feature attributes * notification. flags | = FLAG_AUTO_CANCEL; // click the notification in the notification bar to automatically clear the notification * notification. flags | = FLAG_INSISTENT; // repeat the sound until the user responds to this notification * notification. flags | = FLAG_ONGOING_EVENT; // put this notification in the notification bar, that is, "Ongoing" in the "running" Group * notification. flags | = FLAG_NO_CLEAR; // indicates that this notification is not cleared after you click "clear notification" in the notification bar. * // * notification is often used with FLAG_ONGOING_EVENT. number = 1; // The number field indicates the number of current events represented by this notification. It will overwrite the number at the top of the status bar icon * // if you want to use this field, it must start from 1 * notification. iconLevel = ;//*/
Source code download: Click to download
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.