-1

I made a flashlight app. When I click on the button, app aslo shows a notification. My problem is: I want to close the flashlight and also the notification just clicking on it.

Note: CloseActivity.java is empty

Is there a chance to close the flashlight(or the app) from notification?

public class MainActivity extends AppCompatActivity {

    String content = "Flashlight is on";

    private Camera cam1;
    Camera.Parameters params;
    private Camera.Parameters parameter;
    private boolean isOn;



    private boolean checkAndRequestPermissions() {

        int permissionCAMERA = ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA);

        List<String> listPermissionsNeeded = new ArrayList<>();

        if (permissionCAMERA != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(android.Manifest.permission.CAMERA);
        }

        if (!listPermissionsNeeded.isEmpty()) {
            ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
            return false;
        }
        return true;
    }

    public static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 1905;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (!checkAndRequestPermissions()) {
            return;

        }
        else {
            cam1 = Camera.open();
            parameter = this.cam1.getParameters();

        }


        }



    public void sendNotification(View view){
        final Button btn =(Button) findViewById(R.id.button);
        if (isOn){
            params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
            cam1.setParameters(params);
            cam1.stopPreview();
            isOn=false;
            btn.setBackgroundResource(R.drawable.off);

        }

        else {
            params=cam1.getParameters();
            params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
            cam1.setParameters(params);
            cam1.startPreview();
            isOn=true;
            btn.setBackgroundResource(R.drawable.on);
            switch(view.getId()){

                case R.id.button:
                    addNotification();
                    break;
            }
        }


    }




    private void addNotification() {
        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.notif)
                        .setContentTitle("FlasLight")
                        .setContentText("Tap To Close");

        Intent notificationIntent = new Intent(this, CloseActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(contentIntent);


        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(0, builder.build());
    }
}

enter image description here

Qrchack
  • 899
  • 1
  • 10
  • 20
Bret
  • 11
  • 3
  • 1
    Possible duplicate of [How to set click listener for notification?](http://stackoverflow.com/questions/7184963/how-to-set-click-listener-for-notification) – Qrchack Mar 06 '17 at 15:10

1 Answers1

0

You need to set a click listener on the notification, then disable the flashlight and remove the notification in a callback. See How to set click listener for notification?

Community
  • 1
  • 1
Qrchack
  • 899
  • 1
  • 10
  • 20
  • Could you provide an example? I know that I need a new activity when I click on notification. But I don't have any idea how to close the app with that activity. – Bret Mar 06 '17 at 15:41
  • You don't need to close the app, that's the whole point. You could try [this](http://blog.kibotu.net/java/android-programmatically-close-app-by-pressing-back-button-properly), but this is very much overkill. You force the system to load the whole app from scratch once the user wants to run it away, which violates Android's design - to keep apps in memory and reduce loading time. When you move out of your app to the home screen, the app is already "closed" and not using your CPU, clicking the notification will just "wake it up". TL;DR don't worry about "closing the app" – Qrchack Mar 06 '17 at 15:51