Android-system notification bar message with progress bar

Source: Internet
Author: User

:

There is only one button on the main interface and the file will not be available.
The layout file content_view.xml used is displayed in the notification bar.

<? Xml version = "1.0" encoding = "UTF-8"?>
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: background = "#00000000"
Android: orientation = "vertical"
Android: padding = "5dp">
 
<ImageView
Android: id = "@ + id/content_view_image"
Android: layout_width = "25dp"
Android: layout_height = "25dp"
Android: src = "@ drawable/logo"

/>
<TextView
Android: id = "@ + id/content_view_text1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "0%"
Android: textColor = "#000000"
Android: layout_toRightOf = "@ id/content_view_image"
Android: layout_centerHorizontal = "true"
Android: layout_marginTop = "5dp"
Android: layout_marginLeft = "15dp"
/>
<ProgressBar
Android: id = "@ + id/content_view_progress"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Style = "@ android: style/Widget. ProgressBar. Horizontal"
Android: max = "100"
Android: layout_below = "@ id/content_view_image"
Android: layout_marginTop = "4dp"
/>

</RelativeLayout>

Main running class:

[Java]
Package yyy. testandroid4;
 
Import java. util. Timer;
Import java. util. TimerTask;
 
Import android. app. Activity;
Import android. app. AlertDialog. Builder;
Import android. app. Notification;
Import android. app. icationicationmanager;
Import android. app. PendingIntent;
Import android. content. DialogInterface;
Import android. content. Intent;
Import android. content. pm. PackageManager. NameNotFoundException;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. OS. Message;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. RemoteViews;
Import android. widget. Toast;
 
Public class TestAndroid4Activity extends Activity {


Private Handler handler = new Handler (){
@ Override
Public void handleMessage (Message msg ){
// TODO Auto-generated method stub
Super. handleMessage (msg );
Switch (msg. what ){
Case 0:
Notif. contentView. setTextViewText (R. id. content_view_text1, len + "% ");
Notif. contentView. setProgressBar (R. id. content_view_progress, 100, len, false );
Manager. Policy (0, notif );

Break;
Case 1:
Toast. makeText (TestAndroid4Activity. this, "download completed", 0). show ();
Break;
Default:
Break;
}
}

};

Private Button update, cancel;
Private int localVersion, serverVersion;
Private int len;
Private icationicationmanager manager;
Private Notification notif;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

Update = (Button) findViewById (R. id. update );

Update. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View arg0 ){
// TODO Auto-generated method stub
// Click the activity that opens after the notification bar
Intent intent = new Intent (TestAndroid4Activity. this, OtherActivity. class );

PendingIntent pIntent = PendingIntent. getActivity (TestAndroid4Activity. this, 0, intent, 0 );
Manager = (icationicationmanager) getSystemService (NOTIFICATION_SERVICE );
Notif = new Notification ();
Notif. icon = R. drawable. logo;
Notif. tickerText = "new notification ";
// Display the layout file used in the notification bar
Notif. contentView = new RemoteViews (getPackageName (), R. layout. content_view );
Notif. contentIntent = pIntent;
Manager. Policy (0, notif );
New DownLoadThread (). start ();
}
});



}
}

Private class DownLoadThread extends Thread {
Private Timer timer = new Timer ();
@ Override
Public void run (){
// TODO Auto-generated method stub
Super. run ();
Timer. schedule (new TimerTask (){
@ Override
Public void run (){
// TODO Auto-generated method stub

Message msg = new Message ();
Msg. what = 0;
Msg. obj = len;
Handler. sendMessage (msg );

If (len = 100 ){
Timer. cancel ();
Handler. sendEmptyMessage (1 );
}

}
},0, 1000 );
Len = 0;
Try {
While (len <100 ){
Len ++;
Thread. sleep (1000 );
}
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}

}


}

 


From the Central-Perk Column

Related Article

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.