0

In my IntentService class, i create a Notification and assign ID=1213, and the notification will show up once the apps is open.

    Intent cancelScan = new Intent();
    cancelScan.setAction(CANCEL);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1213, cancelScan, PendingIntent.FLAG_UPDATE_CURRENT);
    mNbuilder.addAction(android.R.drawable.ic_menu_close_clear_cancel,"Cancel Scanning",pendingIntent);

    NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(NOTIFICATION_ID, mNbuilder.build());

in my BroadcastReceiver class

if(CANCEL.equals(intent.getAction())){
        Log.i(TAG,"Received Broadcasr Receiver");
        NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        //context.stopService(new Intent(context,ScanService.class));
        nm.cancel(NOTIFICATION_ID);
    }
}

and, my Manifest.XML

 <receiver android:name=".WifiScanReceiver" android:enabled="true">
        <intent-filter>
            <action android:name="CANCEL"/>
        </intent-filter>
    </receiver>

I had tried couple times when i click the action button beneath the notification, but the Logcat did not print anything. Which parts i had done wrong? Thank in Advance.

noleavename
  • 83
  • 3
  • 15
  • What is the value of `CANCEL` in this line: `cancelScan.setAction(CANCEL)` – 0xDEADC0DE Mar 09 '16 at 15:41
  • private final String CANCEL = "CANCEL", variable and value all i using same name. Thank for your reply :) – noleavename Mar 09 '16 at 15:48
  • Are you also sure that the value of `CANCEL` in your `BroadcastReceiver` is the same? – 0xDEADC0DE Mar 09 '16 at 15:49
  • Yes, i declare as private static final String YES_ACTION = "com.example.packagename.CANCEL"; that "packagename" i had rename to my project name as well. – noleavename Mar 09 '16 at 15:54
  • I don't quite follow. I want to know the value of `CANCEL` on this line: `if(CANCEL.equals(intent.getAction())){`. If that is indeed "com.example.packagename.CANCEL" then this will not work, because your actions do not match – 0xDEADC0DE Mar 09 '16 at 15:56
  • so i need to declare the value of CANCEL in broadcastReceiver class to private final String CANCEL = "CANCEL" also? not private static final String CANCEL = "com.example.packagename.CANCEL"? – noleavename Mar 09 '16 at 16:01
  • Yes, otherwise your comparison will not work because you are actually doing this: `"com.example.packagename.CANCEL".equals("CANCEL")` which is not equal. I will create an answer – 0xDEADC0DE Mar 09 '16 at 16:03
  • just tried, it works. Thank you very much, i will accept the answer once u created. – noleavename Mar 09 '16 at 16:08
  • You're welcome, glad I could help. Added an answer – 0xDEADC0DE Mar 09 '16 at 16:09

1 Answers1

0

The values of CANCEL and the value declared as action ("CANCEL") in your manifest must be equal, or it won't work. Your's don't, that's why you don't reach your if statement. Your receiver is triggered though, because you send a broadcast with the correct action.

To make sure you use the correct values in your code, you can declare a static final in your WifiScanReceiver:

public class WifiScanReceiver {
    public static final String CANCEL = "CANCEL";
    ...
}

So you can also use it in you code when you send the broadcast:

Intent cancelScan = new Intent();
cancelScan.setAction(WifiScanReceiver.CANCEL);

That way you are always certain that you use the same value. The only one that you have to make sure is also correct, is the one in your manifest.

0xDEADC0DE
  • 2,453
  • 1
  • 17
  • 22
  • Thank a lot, because i was refer [link](http://stackoverflow.com/questions/15350998/determine-addaction-click-for-android-notifications) here – noleavename Mar 09 '16 at 16:13