Android Studio 中服务类的 onCreate 方法中未知的构造函数参数

问题描述 投票:0回答:0
``package com.arkenstonestudio.docxscanner.Notification;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import androidx.core.app.NotificationCompat;
import com.arkenstonestudio.docxscanner.LauncherActivity;
import com.arkenstonestudio.docxscanner.R;

public class ShowNotification extends Service {

    private final static String TAG = "ShowNotification";
    NotificationManager mNotificationManager;
    private int presetOption = 0;

    public void notificationValues(int presetOptionX){

       this.presetOption = presetOptionX; // 3 int value here is not known in onCreate
        Log.i(TAG, "Constructor: "+ this.presetOption);

    }

    @Override
    public void onCreate() {
        super.onCreate();

        Log.i(TAG, "onCreate: "+presetOption); // 0 int value is shown here instead of 3

        //Notification Preset1
        String titledText1 = "Scan Docs With A.I Filters";
        String someBigText1 = "It's been a long time since no document is scanned...";
        if (presetOption == 1)
        {
            createNotification(titledText1, someBigText1);
        }
        else if (presetOption == 2)
        {
            //notification Preset2
            String titledText2 = "Try ID Car Scan";
            String someBigText2 = "Best ever ID card scanning let you scan both side of ID Card on same page more visible";
            createNotification(titledText2, someBigText2);
        }else if (presetOption == 3)
        {
            //notification Preset3
            String titledText3 = "Try Digital Signature";
            String someBigText3 = "Draw or Import digital signature and place it on any document you want.";
            createNotification(titledText3, someBigText3);
        }else if (presetOption == 4)
        {
            //notification Preset4
            String titledText4 = "D.S.P Ocr feature";
            String someBigText4 = "Let's convert text image into editable text for you...";
            createNotification(titledText4, someBigText4);
        }
        else
        {
            createNotification(titledText1, someBigText1);
        }


    }

    private void createNotification(String titledTxt, String bigTxt){

        NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(getApplicationContext(), "notify_001");
        Intent ii = new Intent(getApplicationContext(), LauncherActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, ii, 0);


        mBuilder.setContentIntent(pendingIntent);
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mBuilder.setSmallIcon(R.drawable.docx_scan_trans);
            mBuilder.setColor(getResources().getColor(R.color.white));
        } else {
            mBuilder.setSmallIcon(R.mipmap.ic_launcher);
        }

        mBuilder.setContentTitle(titledTxt);
        mBuilder.setContentText(bigTxt);
        mBuilder.setPriority(Notification.PRIORITY_MAX);
        mBuilder.setStyle(bigText);

        mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // === Removed some obsoletes
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            String channelId = "dsp-rc-123";
            NotificationChannel channel = new NotificationChannel(
                    channelId,
                    "Docx scanner Plus",
                    NotificationManager.IMPORTANCE_HIGH);
            mNotificationManager.createNotificationChannel(channel);
            mBuilder.setChannelId(channelId);
        }

        mNotificationManager.notify(0, mBuilder.build());
        Log.i(TAG, "Notification created");
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}`

` 我在上面的代码中尝试使用构造函数初始化 int 值,然后我尝试在 onCreate 方法中访问该值,但问题是 onCreate 方法中的 int 值始终为 0,而该值在构造函数中正确显示。任何人都可以帮助我......提前致谢。

显示屏幕截图以获得更多说明: enter image description here

我只想访问构造函数值并将其分配给类内的变量,以便使其成为全局变量。

我在 Stack Overflow 上没有找到与我的问题相关的任何合适答案

java android android-studio service android-notifications
© www.soinside.com 2019 - 2024. All rights reserved.