如何在Android上操作状态栏

问题描述 投票:0回答:1

我想创建一个在状态栏上有倒计时器的应用程序,例如绿色 WhatsApp 通话状态。但android并没有授予操作状态栏的权限。 WhatsApp 如何做到这一点? WhatsApp 通话状态

我是在通知栏上完成的。但我想在状态栏上执行此操作(例如时钟所在的位置)。

java android kotlin mobile whatsapp
1个回答
0
投票

我认为你应该尝试以下代码(我还没有测试过,但我认为它应该有效)。

NotificationService.java

public class NotificationService extends Service {

    private static final String CHANNEL_ID = "NotificationServiceChannel";
    private CountDownTimer countDownTimer;
    private long startTime;

    @Override
    public void onCreate() {
        super.onCreate();
        createNotificationChannel();
        startTime = System.currentTimeMillis();
        // Start countdown timer for 60 seconds (60000 milliseconds)
        startCountDownTimer(60000);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Notification notification = createNotification("00:00");
        startForeground(1, notification);
        return START_NOT_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
        super.onDestroy();
    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel serviceChannel = new NotificationChannel(
                CHANNEL_ID,
                "Call Service Channel",
                NotificationManager.IMPORTANCE_DEFAULT
            );
            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(serviceChannel);
        }
    }

    private Notification createNotification(String time) {
        return new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Ongoing Call")
                .setContentText(time)
                .setSmallIcon(R.drawable.ic_notification)
                .build();
    }

    private void startCountDownTimer(long duration) {
        countDownTimer = new CountDownTimer(duration, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                long elapsedTime = duration - millisUntilFinished;
                long minutes = TimeUnit.MILLISECONDS.toMinutes(elapsedTime);
                long seconds = TimeUnit.MILLISECONDS.toSeconds(elapsedTime) % 60;
                String time = String.format("%02d:%02d", minutes, seconds);

                Notification notification = createNotification(time);
                NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                notificationManager.notify(1, notification);
            }

            @Override
            public void onFinish() {
                // Optional: Handle what happens when the timer finishes
                stopSelf();
            }
        }.start();
    }
}

ic_notification.xml

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24"
    android:viewportHeight="24">
    <path
        android:fillColor="@android:color/black"
        android:pathData="M19.48,15.88l-2.59-2.59c-0.39-0.39-1.02-0.39-1.41,0l-1.69,1.69c-0.39,0.39-0.39,1.02,0,1.41l2.59,2.59c0.39,0.39,1.02,0.39,1.41,0l1.69-1.69C19.87,16.9,19.87,16.27,19.48,15.88zM10.9,12.5c-1.41,0-2.73-0.53-3.73-1.5l-0.75-0.75c-0.39-0.39-1.02-0.39-1.41,0l-1.69,1.69c-0.39,0.39-0.39,1.02,0,1.41l0.75,0.75c1.62,1.62,3.99,2.63,6.4,2.63c4.4,0,7.8-3.4,7.8-7.8c0-1.41-0.4-2.73-1.07-3.86c-0.17-0.31-0.55-0.43-0.86-0.29l-1.44,0.58c-0.43,0.17-0.64,0.65-0.47,1.08c0.37,0.86,0.59,1.83,0.59,2.82c0,3.27-2.66,5.9-5.9,5.9z" />
</vector>

您可以在任何活动中调用此服务,如下所示:

public class YourActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Start the NotificationService
        Intent serviceIntent = new Intent(this, NotificationService.class);
        startService(serviceIntent);
    }
}

还要确保

NotificationService.java
在您的
AndroidManifest.xml
中拥有必要的权限。将以下内容添加到您的
AndroidManifest.xml

<service
    android:name=".NotificationService"
    android:foregroundServiceType="connectedDevice"
    android:enabled="true"
    android:exported="false" />

我认为这会解决您的问题并创建像 Whatsapp 这样的通知。 如果我的实现中有任何不正确的地方,请纠正我,我将非常感激。

© www.soinside.com 2019 - 2024. All rights reserved.