在Android Studio的Service Class的onCreate方法中构造函数参数未知


``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,而值在构造函数内正确显示。任何人都可以帮我……提前感谢。

截图显示更多的说明:输入图片描述

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

我没有找到任何与我的问题有关的合适答案

与其使用构造函数在应用程序上下文中传递某个值,不如尝试将其作为Bundle传递给OnCreate函数。这里有一个文档链接:https://developer.android.com/reference/android/os/Bundle。"@Overridepublic void onCreate(Bundle putYourInfoHere) {super.onCreate (putYourInfoHere);

this.presetOption = putYoutInfoHere.getInt
//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);
}

}`

我假设你得到你的presetValue在你的构造函数从另一个页面。如果是这样,您应该实现一个函数来将值存储在您的Bundle中,如文档中所示。

最新更新