gpt4 book ai didi

android - 我不能在 onMessageReceived 中使用 AlertDialog

转载 作者:行者123 更新时间:2023-12-04 17:52:11 26 4
gpt4 key购买 nike

我尝试在 onMessageReceived 中使用 AlertDialog,但出现此错误。

05-04 11:27:40.038 30721-31424/com.xxx.xxx E/AndroidRuntime: FATAL EXCEPTION: pool-4-thread-1
Process: com.xxx.xxx, PID: 30721
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:200)
at android.os.Handler.<init>(Handler.java:114)
at android.app.Dialog.<init>(Dialog.java:108)
at android.app.AlertDialog.<init>(AlertDialog.java:125)
at android.app.AlertDialog$Builder.create(AlertDialog.java:967)
at android.app.AlertDialog$Builder.show(AlertDialog.java:986)
at com.xxx.xxx.util.FirebaseMessagingService.optionalAlert(FirebaseMessagingService.java:78)
at com.xxx.xxx.util.FirebaseMessagingService.onMessageReceived(FirebaseMessagingService.java:38)
at com.google.firebase.messaging.FirebaseMessagingService.zzl(Unknown Source)
at com.google.firebase.messaging.FirebaseMessagingService.zzJ(Unknown Source)
at com.google.firebase.messaging.FirebaseMessagingService.handleIntent(Unknown Source)
at com.google.firebase.iid.zzb$1.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)

这是我的代码:

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

super.onMessageReceived(remoteMessage);
String msg=remoteMessage.getNotification().getBody();
Map<String, String> params = remoteMessage.getData();
String Valuekey = remoteMessage.getData().get("key");

if (Valuekey.equals("true")) {
optionalAlert();
}else {
Util.Alert(this,getString(R.string.ConsignmentNoFound));
}

// Util.Alert(getBaseContext(),msg);
}

private void optionalAlert () {


AlertDialog.Builder adb = new AlertDialog.Builder(this);

TextView textView = new TextView(this);
textView.setText(getString(R.string.ConsignmentFound));
textView.setTextSize(24);
adb.setCustomTitle(textView);


//adb.setTitle(getString(R.string.ConsignmentFound));


adb.setIcon(android.R.drawable.ic_dialog_alert);


adb.setPositiveButton("Si", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {

Intent nextActivity = new Intent(FirebaseMessagingService.this, MainActivity.class);
nextActivity.putExtra("act", "PinValidate");
startActivity(nextActivity);

} });


adb.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {


} });
adb.show();
}

}

最佳答案

这肯定有效

package pkg.name

import pkg.name.DisplayAlert
import android.content.Intent
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
class CloudMsg: FirebaseMessagingService() {
override fun onNewToken(token: String) {}

override fun onMessageReceived(remoteMessage: RemoteMessage) {
val mDisplayAlert = Intent(this, DisplayAlert::class.java)
mDisplayAlert.flags = Intent.FLAG_ACTIVITY_NEW_TASK
mDisplayAlert.putExtra("title", remoteMessage.notification?.title)
mDisplayAlert.putExtra("body", remoteMessage.notification?.body)
startActivity(mDisplayAlert)
}

override fun onCreate() {}

}

package pkg.name

import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity

class DisplayAlert : AppCompatActivity() {
internal var title: String? = null
internal var body: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
b = intent.extras
if (b != null) {
title = b!!.getString("title")
body = b!!.getString("body")
val builder: AlertDialog.Builder = AlertDialog.Builder(this)
builder.setTitle(title)
builder.setIcon(R.drawable.store)
builder.setMessage(body)
.setCancelable(false)
.setPositiveButton("Yes") { dialog, id ->
finish()
}
val alertDialog = builder.create()
alertDialog.show()
}
}
companion object {
var b: Bundle? = null
}
}

<service
android:name=".CloudMsg"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>

<activity
android:name=".DisplayAlert"
android:label="@string/app_name"
android:theme="@style/Total_Translucent">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

<style name="Total_Translucent" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="android:windowBackground">@color/transparent</item>
<item name="android:windowIsTranslucent">true</item>
<item name="colorPrimaryDark">@color/transparent</item>
</style>

<color name="transparent">@android:color/transparent</color>

enter image description here

关于android - 我不能在 onMessageReceived 中使用 AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43788352/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com