【问题标题】:How to send a Toast from a Notification after performing an action on click in Android?在 Android 中执行点击操作后,如何从通知中发送 Toast?
【发布时间】:2013-12-28 06:47:40
【问题描述】:

我有一个应用程序,通过单击通知中的操作按钮来发送 SMS。我想在发送 SMS 后显示 Toast,但这不起作用。恐怕它与 AsyncTask 和/或 BroadcastReceiver 有关。

发送 SMS 的工作流程如下所示:

  • 我在AsyncTask 中扫描我的设备联系人并从AsyncTaskonPostExecute() 创建Notification
  • 我使用NotificationCompat.Builder 创建Notification
  • 我将PendingIntent 添加到Notification,如下所示:

    PendingIntent.getBroadcast(mContext, (int) _person.getId(), i, PendingIntent.FLAG_ONE_SHOT);

  • 我从BroadcastReceiveronReceive() 发送短信

  • 我尝试像这样从那里发送 Toast:

    Toast.makeText(_context, "SMS sent!", Toast.LENGTH_SHORT).show();

我尝试对此进行调试,但不幸的是 Eclipse 没有向我显示 onReceive() 中的变量内容。

我还阅读了有关 Google 开发者网站 here 通知的“处理兼容性”的内容,但我找不到更详细解释的教程。

编辑:

我想解释一下通过所有类传递了什么 Context 可能会有所帮助:

  • 在我的MainActivity 中,我将PreferenceFragment 显示为主要内容
  • 在那个PreferenceFragment 中,我调用了新的MyAsyncTask(getActivity()).execute();,所以我的MainActivity 应该是我的Context
  • MyAsyncTask 的构造函数中,我将其传递给创建Notification 的类并将其保存为成员(mContext)
  • 我已经提到的其余部分...

编辑 2:

这是我的BroadcastReceiver 的相关代码,用于监听Notification 的操作:

@Override
public void onReceive(Context _context, Intent _intent)
{
    String type = _intent.getStringExtra("type");
    if (type.equals("SMS"))
    {
        String phoneNumber = _intent.getStringExtra("phoneNumber");
        String message = _context.getResources().getString(
            R.string.smstext);
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(phoneNumber, null, message, null, null);

        Toast.makeText(_context, "SMS sent!", Toast.LENGTH_SHORT).show();           
    }
}

【问题讨论】:

  • 有错误信息吗?如果有请张贴。 _context 是什么,它在哪里设置?
  • 请详细说明“这不起作用”
  • 好的,SMS 已发送,但 Toast 未显示。 _context 只是从 onReceive(Context _context, Intent _intent) 传递过来的 Context。 (我对传递的变量使用下划线)我没有收到错误消息。
  • 一次使用“mContext”和一次“_context”是否正常?这些都一样吗。您可能将错误的上下文传递给您的 toast
  • mContext 是我在创建通知的类中使用的成员变量。最好添加一些关于上下文来自何处的解释。

标签: android android-asynctask android-notifications toast android-context


【解决方案1】:

使用可运行文件。每当我在线程中执行命令时,我都会设置一个处理程序和一个可运行对象,让我在一切完成后在主线程中运行:

// Declare a global handler for the class
final Handler mHandler = new Handler();
// Declare a runnable that will do things app-side when your thread is finished
final Runnable mMessageSent= new Runnable()
{
    @Override
    public void run()
    {
        Toast.makeText(_context, "SMS sent!", Toast.LENGTH_SHORT).show();
    }
};

// Set up your thread to post the runnable when it's finished sending an SMS
private void sendMessage()
{
    Thread t = new Thread()
    {
        public void run()
        {
            // Send your SMS here
            // When finished, notify the handler so it knows to show a toast notification
            mHandler.post(mMessageSent);
        }
    };
    t.start();
}

【讨论】:

  • 不幸的是,这不起作用。我之前尝试过基本相同,也没有工作。不过还是谢谢!
【解决方案2】:

MasterKale 已关闭,问题是您需要从 UI 线程发送 toast,否则将无法正常工作。我不确定他认为他在用那个线程做什么。

final Handler mHandler = new Handler(Looper.getMainLooper());

mHandler.post(new Runnable(){
        @Override
        public void run() {
            Toast.makeText(mContext, "Message", Toast.LENGTH_SHORT).show();
        }

});

【讨论】:

  • 不幸的是,这也不起作用。我编辑了我的帖子并添加了我的BroadcastReceiveronReceive()。我用你的代码用Toast 替换了该行(必须将mContext 更改为_context 并且必须使其变为final)。但它仍然没有工作。这让我发疯了。不知何故,Context 不会错,因为我可以访问应用程序的Resources
  • 尝试将您的逻辑移动到服务中,让您的接收器启动服务以发送短信并显示敬酒。
  • 您的意思是不使用AsyncTask 扫描联系人?或者只是在接收者的onReceive()中发生的部分?
  • 只是发生在 onReceive 中的部分,我认为您遇到了生命周期问题。 developer.android.com/reference/android/content/…
  • 我将代码移至服务并从BroadcastReceiveronReceive() 调用它。但它似乎没有得到正确的上下文。我将getApplicationContext() 传递给Toast.makeText(),但它不起作用。
猜你喜欢
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多