首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

在闹钟接收器android上设置通知

在 Android 闹钟接收器上设置通知,可以通过以下步骤实现:

步骤1:准备阶段

在开始之前,请确保你拥有以下设备和软件:

  • Android 设备或模拟器
  • Android Studio 或其他 Android 开发 IDE
  • ADB(Android Debug Bridge)

步骤2:设置闹钟接收器

首先,需要在 Android 项目的 MainActivity 中声明和注册 AlarmReceiver

代码语言:java
复制
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // 代码逻辑
    }
}

步骤3:设置通知

创建一个布局文件(如果还没有),例如 notification_layout.xml。在该布局文件中,添加一个 NotificationCompat.Builder,用于创建通知。

代码语言:xml
复制
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/notification_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- 在这里添加其他视图(如图像、文本等) -->

    <NotificationCompat.Builder
        android:id="@+id/notification_builder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Hello World!"
        android:showWhen="true" />

</RelativeLayout>

接下来,在 MainActivity 中创建并显示通知:

代码语言:java
复制
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private AlarmReceiver alarmReceiver;
    private NotificationCompat.Builder notificationBuilder;

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

        alarmReceiver = new AlarmReceiver();

        Button toggleButton = findViewById(R.id.toggleButton);
        toggleButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toggleNotification();
            }
        });

        notificationBuilder = new NotificationCompat.Builder(this);
        notificationBuilder.setSmallIcon(R.drawable.ic_launcher_foreground);
        notificationBuilder.setContentTitle("My notification");
        notificationBuilder.setContentText("Hello World!");
        notificationBuilder.setDefaults(NotificationCompat.DEFAULT_ALL);
        notificationBuilder.setWhen(System.currentTimeMillis());

        Intent notificationIntent = new Intent(this, MainActivity.class);
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        notificationIntent.setAction(Intent.ACTION_MAIN);
        notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notificationBuilder.setContentIntent(contentIntent);

        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
        notificationManagerCompat.notify(1, notificationBuilder.build());
    }

    private void toggleNotification() {
        if (notificationBuilder != null) {
            notificationBuilder.setDefaults(NotificationCompat.DEFAULT_ALL);
            notificationBuilder.setWhen(System.currentTimeMillis());
            NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
            notificationManagerCompat.notify(1, notificationBuilder.build());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(alarmReceiver);
    }
}

步骤4:设置服务以处理闹钟

创建

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券