``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上没有找到任何与我的问题相关的合适答案
1条答案
按热度按时间ac1kyiln1#
与其使用构造函数在应用程序上下文中传递某个值,不如尝试将其作为Bundle传递给OnCreate函数。这里是文档的链接:https://developer.android.com/reference/android/os/Bundle . ` @Override public void onCreate(Bundle putYourInfoHere){ super.onCreate(putYourInfoHere);
我假设你从另一个页面获得了你在构造函数中的presetValue。如果是这样,你应该实现一个函数来将值存储在你的Bundle中,如文档所示。