【问题标题】:How to start method from notification action?如何从通知操作开始方法?
【发布时间】:2014-05-13 17:09:01
【问题描述】:

在我的主类中,我有 notificationBuilder,它在按钮单击时启动。

        NotificationManager NotifyMgr =
            (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    PendingIntent turnOffIntent = PendingIntent.getBroadcast(this, 0,
            new Intent(NotifiReceiver.turnOffFlash), 0);
    PendingIntent contentIntent = PendingIntent.getActivity(this,
            0, new Intent(this, MainActivity.class), 0);
    NotificationCompat.Builder builder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_lightbulb_on)
                    .setContentTitle(getString(R.string.app_name))
                    .setContentText(getString(R.string.app_name))
                    .setContentIntent(contentIntent)
                    .setDefaults(Notification.DEFAULT_ALL) // requires VIBRATE permission
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(getString(R.string.app_name) + "dfddddddggffgf"))
                    .addAction(R.drawable.ic_lightbulb,
                            getString(R.string.app_name), turnOffIntent);
    NotifyMgr.notify(notificationId, builder.build());

这里是 NotifiReceiver.class

    package com.AleXMan.torch.standart;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class NotifiReceiver extends BroadcastReceiver {

    MainActivity mna = new MainActivity();
    public static final String turnOffFlash = "com.AleXMan.torch.standart.turnOffFlash";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("test", "recieved");
        if (intent.getAction().equals(turnOffFlash)) {
            mna.toggleFrstFlash();
            Log.i("test", "confirmed");
        }
        throw new UnsupportedOperationException("Not yet implemented");
    }
}

但是当我点击通知中的这个“按钮”时:.addAction(R.drawable.ic_lightbulb, getString(R.string.app_name), turnOffIntent); 什么都没有发生。
你能告诉我我到底做错了什么吗?
非常感谢您,对您未来的帮助!

【问题讨论】:

  • 请发布您的清单,为您的NotifiReceiver 显示<receiver> 元素。
  • 这是我错过的东西!!你能在回答器中写下我需要添加到清单吗?

标签: android methods notifications android-activity broadcastreceiver


【解决方案1】:

鉴于您当前的代码,您需要将其作为清单中 <application> 元素的子元素:

<receiver android:name="NotifiReceiver">
  <intent-filter>
    <action android:name="com.AleXMan.torch.standart.turnOffFlash" />
  </intent-filter>
</receiver>

但是,更好的方法是将new Intent(NotifiReceiver.turnOffFlash) 替换为new Intent(this, NotifiReceiver.class)。然后,您可以:

<receiver android:name="NotifiReceiver"/>

减少打字次数并提高安全性。

请注意,这假定根 &lt;manifest&gt; 元素中的 packagecom.AleXMan.torch.standart

【讨论】:

  • @user2186218:不适用于您在此处显示的代码。
  • 但是现在,当我在通知中按下按钮时它崩溃了,说:进程:com.AleXMan.torch.standart,PID:30894 java.lang.RuntimeException:无法启动接收器 com.AleXMan.torch .standart.NotifiReceiver: java.lang.UnsupportedOperationException: 尚未实现
  • @user2186218:这就是您的接收器所做的。您可以通过阅读您自己的源代码并查看其中的throw new UnsupportedOperationException("Not yet implemented"); 行来判断这一点。
猜你喜欢
  • 1970-01-01
  • 2017-09-06
  • 1970-01-01
  • 1970-01-01
  • 2019-07-16
  • 1970-01-01
  • 2018-01-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多