英文:
Constructor parameters not known inside onCreate method of Service Class in Android Studio
问题
以下是您要翻译的代码部分:
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; // 在此处设置 int 值,但 onCreate 中显示的是 0 而不是 3
Log.i(TAG, "Constructor: "+ this.presetOption);
}
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "onCreate: "+presetOption); // 在这里显示的是 0,而不是 3
// 通知 Preset1
String titledText1 = "使用 A.I 过滤器扫描文档";
String someBigText1 = "已经很长时间没有扫描文档了...";
if (presetOption == 1)
{
createNotification(titledText1, someBigText1);
}
else if (presetOption == 2)
{
// 通知 Preset2
String titledText2 = "尝试身份证扫描";
String someBigText2 = "最好的身份证扫描,可以在同一页面上扫描身份证的两面更清晰可见";
createNotification(titledText2, someBigText2);
}else if (presetOption == 3)
{
// 通知 Preset3
String titledText3 = "尝试数字签名";
String someBigText3 = "绘制或导入数字签名,并将其放在任何您想要的文档上。";
createNotification(titledText3, someBigText3);
}else if (presetOption == 4)
{
// 通知 Preset4
String titledText4 = "D.S.P OCR 功能";
String someBigText4 = "让我们将文本图像转换为可编辑的文本...";
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.Style(bigText);
mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// === 删除了一些过时的部分
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;
}
}
请注意,我已经将代码部分进行了翻译,但是不包括代码之外的内容。如果您需要更多帮助或有其他问题,请随时提问。
英文:
``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;
}
}`
`
I am trying above code where I use constructor for initialization of int value and then I try to access that value inside onCreate method but the problem is that int value inside onCreate method is always 0 while the value is shown correctly inside constructor. Anyone can help me with that...Thanks in Advance.
Screenshot is shown for more clarification:
enter image description here
All I want to access the constructor value and assign it to variable inside class so that to make it global.
I did not find any suitable answer related to my problem on Stack Overflow
答案1
得分: 1
以下是翻译好的部分:
"Instead of using the constructor to pass a certain value in an application context, you should try to pass it as a Bundle to the OnCreate function. Here is a link to the documentation: https://developer.android.com/reference/android/os/Bundle .
我假设你在构造函数中获取你的 presetValue 从另一页。如果是这样的话,你应该实现一个函数,将该值存储在 Bundle 中,如文档中所示。"
英文:
Instead of using the constructor to pass a certain value in an application context, you should try to pass it as a Bundle to the OnCreate function. Here is a link to the documentation : https://developer.android.com/reference/android/os/Bundle . ` @Override
public 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);
}
}`
I am assuming that you get you're presetValue in your constructor from another page. If so, you should implement a function to store the value in your Bundle as shown in the documentation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论