how to create a Progress bar notification in android studio and performing task of Downloading any file from server
how to create a Progress bar notification in android studio. this type of notification is used when we are performing any continues task link Downloading any file from server
step :1 create a App class and create an notification channels
public class App extends Application {
public static final String CHANNEL_ID = "channel1";
@Override
public void onCreate() {
super.onCreate();
creatnotificationchannel();
}
private void creatnotificationchannel() {
if (Build.VERSION.SDK_INT >=
Build.VERSION_CODES.O) {
CharSequence name = "personal
notification";
String description = "personal
informaton all are here";
int importance =
NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel
notificationChannel = new NotificationChannel(CHANNEL_ID, name,
importance);
notificationChannel.setDescription(description);
NotificationManager
notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
}
}
}
step :2 add app class into menifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.notifiaction">
<application
android:name=".App"
// This is app class
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
step 3: progress bar notification here we are using Runnable() to perform a runnable task and update progress data . on complete this we show a Complete notification that show task is completed
public void N7_simplenotification(View view) {
final int ProcessmaX = 100;
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setContentTitle("Downloading");
builder.setSmallIcon(R.drawable.ic_file_download_black_24dp);
builder.setContentText("Phir Hera Pheri Full HD 2010 Movie");
builder.setOngoing(true);
builder.setProgress(ProcessmaX, 0, false);
builder.setPriority(NotificationCompat.PRIORITY_LOW);
final NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
notificationManagerCompat.notify(1, builder.build());
new Thread(new Runnable() {
@Override
public void run() {
SystemClock.sleep(100);
for (int p = 0; p <= ProcessmaX; p += 10) {
builder.setProgress(ProcessmaX, p, false);
notificationManagerCompat.notify(1, builder.build());
SystemClock.sleep(1000);
}
builder.setContentText("Download finished");
builder.setProgress(0, 0, false);
builder.setOngoing(false);
notificationManagerCompat.notify(1, builder.build());
}
}).start();
}
Comments
Post a Comment