用Android stdio写一个系统通知服务程序,一个按钮发送通知,一个按钮取消通知;写出Java代码和xml代码,和权限代码,及具体步骤
时间: 2024-03-24 09:36:44 浏览: 110
好的,下面是实现系统通知服务程序的Java代码和xml代码,以及相应的权限代码和具体步骤:
Java代码:
```
package com.example.notificationdemo;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private static final String CHANNEL_ID = "channel_id";
private static final int NOTIFICATION_ID = 1;
private Button mBtnSendNotification;
private Button mBtnCancelNotification;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createNotificationChannel();
mBtnSendNotification = findViewById(R.id.btn_send_notification);
mBtnCancelNotification = findViewById(R.id.btn_cancel_notification);
mBtnSendNotification.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendNotification();
}
});
mBtnCancelNotification.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cancelNotification();
}
});
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
private void sendNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(getString(R.string.notification_title))
.setContentText(getString(R.string.notification_text))
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
private void cancelNotification() {
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.cancel(NOTIFICATION_ID);
}
}
```
xml代码:
```
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<Button
android:id="@+id/btn_send_notification"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btn_send_notification"/>
<Button
android:id="@+id/btn_cancel_notification"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btn_cancel_notification"/>
</LinearLayout>
```
权限代码:
```
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
```
具体步骤:
1. 在Android Studio中创建一个新的项目
2. 在res/values/strings.xml文件中添加以下内容:
```
<string name="channel_name">My Notification Channel</string>
<string name="channel_description">My Notification Channel Description</string>
<string name="notification_title">My Notification Title</string>
<string name="notification_text">My Notification Text</string>
<string name="btn_send_notification">Send Notification</string>
<string name="btn_cancel_notification">Cancel Notification</string>
```
3. 在res/drawable文件夹中添加一个名为ic_notification.png的图标作为通知的小图标
4. 在AndroidManifest.xml文件中添加以下内容:
```
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
```
5. 在res/layout文件夹中创建一个名为activity_main.xml的布局文件,并添加以下内容:
```
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<Button
android:id="@+id/btn_send_notification"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btn_send_notification"/>
<Button
android:id="@+id/btn_cancel_notification"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btn_cancel_notification"/>
</LinearLayout>
```
6. 在MainActivity.java文件中添加上述Java代码
7. 运行程序,点击“Send Notification”按钮发送通知,点击“Cancel Notification”按钮取消通知
阅读全文