【发布时间】:2020-01-03 05:49:36
【问题描述】:
我想更新来自另一个活动的通知,并在服务类上声明它,我也制作日志是打印平均方法调用但无法更新通知进度条值。
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_();
}
}
我想从另一个活动类更新通知进度,那我该怎么办?
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);
}
在我的服务类中,我声明了以下方法,我得到了日志值但无法更新通知进度
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());
}
如果有人对此有其他解决方案,请分享您的答案
我的自定义 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>
我的服务类初始化清单
<service
android:name=".Acivity.ForegroundService"
android:enabled="true"
android:exported="true"></service>
【问题讨论】:
-
你在使用 contentView 自定义吗?
-
@PrafulKorat 你在哪里初始化
myNotyficationService? -
@PrafulKorat 我的意思是你在哪里初始化 HomeActivity 中的
myNotyficationService变量。因为从你的代码来看,这行myNotyficationService.updateNoty(50);会让你的应用崩溃。 -
@PrafulKorat 在 Android 中,您不能创建像
myNotyficationService= new MyNotyficationService();这样的服务,并在其上调用方法。首先,您需要在Android系统上注册服务,然后调用startService()或startForegroundService(),然后Android将为您创建并启动服务。如果您想更新进度条的状态,那么您可以从您的活动发送广播到服务,然后服务将更新进度。 -
@PrafulKorat 正如我所说,您不能创建服务实例,在这种情况下,您应该向您的服务发送广播,而不是创建服务并在其上调用
updateNotify()方法。
标签: java android notifications