创建一个名为“创建服务”的程序,该程序一启动服务就会在状态栏显示一条通知。

创建一个名为“创建服务”的程序,该程序一启动服务就会在状态栏显示一条通知。
下面的代码能运行,但是通知栏没有显示通知,并且运行显示了这个,不知道是哪里的问题,求解答

img

这是给的案例图

img

img

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开启服务"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

MyService.java

package com.example.myservice;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

public class MyService extends Service {

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

    @Override
    public void onCreate() {
        super.onCreate();
        final NotificationManager notification = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        // 创建Notification对象
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.drawable.advise2);
        builder.setTicker("显示第一个通知");
        builder.setContentTitle("标题");
        builder.setContentText("我是在服务中创建的Notification");
        builder.setWhen(System.currentTimeMillis()); //发送时间
        builder.setDefaults(Notification.DEFAULT_ALL);
        notification.notify(123, builder.build());
    }

}

MainActivity.java

package com.example.myservice;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

        final Intent intent = new Intent(MainActivity.this,MyService.class);
        Button start = (Button) findViewById(R.id.btn_start);

    }
    //开启服务的方法
    public void start(View view){
        Intent intent = new Intent(this,MyService.class);
        startService(intent);
    }

    @Override
    protected void onStart() {
        startService(new Intent(MainActivity.this,MyService.class)); //开机启动
        super.onStart();
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.myservice">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyService"
        tools:targetApi="31">
        <service android:name=".MyService"
            android:enabled="true"
            android:exported="true">
        </service>


        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

ps:这是teacher给的代码,但是我自己运行不出来
“创建服务”程序对应的布局文件activity_main.xml代码如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    <Button
        android:id="@+id/btn_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="开启服务"
        android:textColor="@android:color/white"
        android:background="@drawable/soil_backgroud_content3" />
</RelativeLayout>

2、创建服务(MyService)
接下来创建一个类继承自Service,并在该类中的onCreate()方法中向状态栏发送通知,具体代码如下所示:

public class MyService extends Service {
     @Override
         public IBinder onBind(Intent intent) {
         return null;
         }
        @Override
         public void onCreate() {
         super.onCreate();
            // 创建Notification对象
             Notification notification = new Notification(R.drawable.ic_launcher,
                     "我是状态栏通知", System.currentTimeMillis());
             // 设置点击Notification就进入主界面
             Intent intent = new Intent(this, MainActivity.class);
             PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                     intent, 0);
             notification.setLatestEventInfo(this, "标题", 
                                             "我是在服务中创建的Notification",pendingIntent);
             // 让Myservice变成前台服务,并在系统状态栏显示出来
             startForeground(1, notification);
         }
     } 

应该是通知权限的问题