答案 0 :(得分:1)
我认为在状态栏中放置一个按钮违反了设计准则,因为它应该只包含通知和系统图标。参见https://material.io/design/platform-guidance/android-bars.html#status-bar。
请改用提示通知(https://developer.android.com/guide/topics/ui/notifiers/notifications.html)。 您可以将按钮放在通知(https://material.io/design/platform-guidance/android-notifications.html#anatomy-of-a-notification)的操作区域中。
答案 1 :(得分:0)
看起来您需要带有通知图标的前台服务- here
前台服务的示例代码-
public class ExampleService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String input = intent.getStringExtra("inputExtra");
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Example Service")
.setContentText(input)
.setSmallIcon(R.drawable.ic_android)
.setContentIntent(pendingIntent)
.build();
startForeground(1, notification);
//do heavy work on a background thread
//stopSelf();
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}