简体   繁体   English

进度条未在通知中更新

[英]Progress bar not update in notification

I want to update notification from another activity and I declare it on the service class, I make log also a log is print mean method call but can't update notification progress bar value.我想更新来自另一个活动的通知,并在服务类上声明它,我还制作了一个日志,打印平均方法调用,但无法更新通知进度条值。

MyNotyficationService class below and my notification code write in this class.下面的MyNotyficationService类和我的通知代码写在这个类中。


public class MyNotyficationService extends Service {
    public static final String NOTIFICATION_CHANNEL_ID = "channelForground";
    public static final String NOTIFICATION_CHANNEL_NAME = "com.example.notificationdemo";
    NotificationCompat.Builder notification;
    NotificationChannel notificationChannel;
    NotificationManager manager;
    RemoteViews contentView;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(final Intent intent, int flags, int startId) {
        contentView = new RemoteViews("com.example.android", R.layout.notification_layout_custom);

        showNotification("0");

        return START_NOT_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    public void updateNoty(int per){
        Log.d("CALL__m","call" +per);
        contentView.setTextViewText(R.id.battery_level_Status, "Downloading : " + per + "%");
        contentView.setProgressBar(R.id.progressBar_notification_current_battery_level, 100, per, false);
        manager.notify(42, notification.build());
    }

    public void showNotification(String body) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
            manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

            if (manager != null) {
                manager.createNotificationChannel(notificationChannel);
            }
        }

        contentView.setTextViewText(R.id.battery_level_Status, "Downloading : " + body + "%");
        contentView.setProgressBar(R.id.progressBar_notification_current_battery_level, 100, Integer.parseInt(body), false);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            notification = new NotificationCompat.Builder(getApplicationContext())
                    .setChannelId(NOTIFICATION_CHANNEL_ID)
                    .setOngoing(true)
                    .setOnlyAlertOnce(true)
                    .setSmallIcon(R.drawable.cat_drinking)
                    .setContent(contentView);
        } else {
            notification = new NotificationCompat.Builder(getApplicationContext())
                    .setOngoing(true)
                    .setOnlyAlertOnce(true)
                    .setContent(contentView)
                    .setSmallIcon(R.drawable.cat_drinking);
        }
        startForeground(10, notification.build());
    }
    private void startService_() {
        Intent restartService = new Intent(getApplicationContext(), MyNotyficationService.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForegroundService(restartService);
        } else {
            startService(restartService);
        }
        PendingIntent restartServicePI = PendingIntent.getService(
                getApplicationContext(), 1, restartService,
                PendingIntent.FLAG_ONE_SHOT);
        AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        alarmService.set(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime() + 1000, restartServicePI);
    }


    @Override
    public void onTaskRemoved(Intent rootIntent) {
        startService_();
    }
}

And I want to update notification progress from another activity class, so what I do for that?我想从另一个活动类更新通知进度,那我该怎么办?


public class HomeActivity extends AppCompatActivity {

  public MyNotyficationService myNotyficationService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.full_screen_image);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                Intent serviceIntent = new Intent(HomeActivity.this, ForegroundService.class);
                ContextCompat.startForegroundService(HomeActivity.this, serviceIntent);
            } else {
                startService(new Intent(HomeActivity.this, ForegroundService.class));
            }

        myNotyficationService= new MyNotyficationService();
        myNotyficationService.updateNoty(50);
   }

In my service class, I declare below method and I got log value but can't update notification progress在我的服务类中,我声明了以下方法并获得了日志值但无法更新通知进度

 public void updateNoty(int per){
        Log.d("CALL__m","call" +per);
        contentView.setTextViewText(R.id.battery_level_Status, "Downloading : " + per + "%");
        contentView.setProgressBar(R.id.progressBar_notification_current_battery_level, 100, per, false);
        manager.notify(42, notification.build());
    }

if anyone has another solution for that then share your answer如果有人对此有其他解决方案,请分享您的答案

My Custom ContentView is我的自定义 ContentView 是

<?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="wrap_content"
    android:orientation="vertical"
    android:paddingLeft="15dp"
    android:paddingTop="10dp"
    android:paddingRight="15dp"
    android:paddingBottom="10dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/notification_image_icon"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_centerVertical="true"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:id="@+id/txt_notification_heading"
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@+id/notification_image_icon"
            android:text="@string/app_name"
            android:textColor="#323232" />
    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/battery_level_Status"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/notification_image_icon"
            android:text="50%"
            android:textColor="#323232" />

    </LinearLayout>

    <ProgressBar
        android:id="@+id/progressBar_notification_current_battery_level"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginTop="5dp"
        android:indeterminate="false"
        android:max="100"
        android:progress="50"
        android:progressDrawable="@drawable/notificationprogress" />
</LinearLayout>

My manifest for service class init我的服务类初始化清单

<service
            android:name=".Acivity.ForegroundService"
            android:enabled="true"
            android:exported="true"></service>

This is what i am doing to update notification from service but i am using NotificationCompat.Builder object to set progress and text.这是我正在做的更新来自服务的通知,但我正在使用 NotificationCompat.Builder 对象来设置进度和文本。

  private void raiseNotification(NotificationCompat.Builder b, int counter) {
    b.setContentText("Downloaded " + counter + "% ");
    b.setProgress(100, counter, false);
    b.setOngoing(true);
    if (counter > 99) {
        b.setAutoCancel(true);
        b.setOngoing(false);
        b.setContentTitle("Download complete").setProgress(0, 0, false);
    }
    if (mgr != null) mgr.notify(NOTIFICATION_ID, b.build());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM