gpt4 book ai didi

android - 我可以在 contentObserver 中创建 Alert 和 Toast 吗?

转载 作者:太空宇宙 更新时间:2023-11-03 13:02:05 25 4
gpt4 key购买 nike

我无法编写代码来在 SMS 的 ContentObserver 中创建警报和 Toast。我无法获得我的 ContentObserver 的上下文。有人请帮助我。提前致谢。

 MyObserver yourObserver = new MyObserver(new Handler());

ContentResolver contentResolver = this.getContentResolver();
contentResolver.registerContentObserver(Uri.parse("content://sms"),true, yourObserver);

&

class YourObserver extends ContentObserver {

//Context context = new ContentObserver()
AlertDialog.Builder builder;
AlertDialog alert;
public YourObserver(Handler handler) {
super(handler);
}

@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Toast.makeText(???,"SENT..!!", Toast.LENGTH_LONG).show();
System.out.println("Hey =++++++++++++++++++++++_______+++++++++++++++++++++++++++++");
builder = new AlertDialog.Builder(???);
builder.setMessage("Are you sure??");
builder.setCancelable(false);
builder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(???, "Okay",Toast.LENGTH_LONG).show();
}
});

builder.setNegativeButton("No",new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
alert.cancel();
}
});
alert = builder.create();
alert.show();
}
}

最佳答案

您可以使用 handler.post() 来显示 Toast。

public SMSSentObserver(Handler handler, Context context) {
super(handler);
this.handler = handler;
m_Context = context;
}

@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
handler.post(new Runnable() {

@Override
public void run() {

Toast.makeText(m_Context, "Toast", Toast.LENGTH_SHORT).show();
}
});

关于android - 我可以在 contentObserver 中创建 Alert 和 Toast 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9937285/

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