我想更新另一个活动的通知,并在服务类上声明它,我也将日志也记录为打印均值方法调用,但无法更新通知进度栏值。
下面的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>
参考方案
这是我从服务更新通知的过程,但是我使用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());
}
android studio的设计预览中的布局为空 - java我一直在从事一个项目,有一天我的布局在android studio设计中显得空白。我已经尝试过放置“ Base”。在文件style.xml中的“ Theme.AppCompat.Light.DarkActionBar”之前,我还尝试清理项目并使缓存/重新启动无效,但没有任何效果。我在manifest.xml中有此代码<?xml version=…
为什么我的应用在启动时总是崩溃-Android - java刚刚遵循了有关如何制作按钮和活动的youtube指南。我按照他的代码减小字体大小,并且在启动时一直崩溃。有人知道为什么吗?public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { su…
不需要选择回收站查看项目 - javaviewHolder.optiontxt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { viewHolder.optiontxt1.setCompoundDrawablesWithIntrinsicBounds(R.dra…
如何自定义Xamarin形式的滑块? - java我要自定义滑块,使其看起来如下图所示:我在.xaml文件中的代码:<Slider MaximumTrackColor="Black" MinimumTrackColor="Black" HeightRequest="50" ThumbColor="Black" ThumbI…
Android Firebase:将数据保存到数据库 - java我正在尝试从Firebase保存和检索数据,但是在获取正确的语法时遇到了一些麻烦。用户通过电子邮件和密码的身份验证方法进行注册,并使用相同的详细信息登录。我有两个编辑文本字段,要求输入名称和语句。在两个编辑文本框下面有一个按钮,用于将数据保存到数据库。private void saveQuote(){ String name = author.getText…