【问题标题】:How to close an android app from notification bar如何从通知栏关闭安卓应用
【发布时间】:2017-03-07 00:48:24
【问题描述】:

我做了一个手电筒应用。当我单击按钮时,应用程序也会显示通知。我的问题是:我想关闭手电筒以及点击它的通知。

注意:CloseActivity.java 为空

是否有机会从通知中关闭手电筒(或应用程序)?

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());
    }
}

【问题讨论】:

标签: java android android-studio


【解决方案1】:

您需要在通知上设置点击监听器,然后禁用手电筒并在回调中移除通知。见How to set click listener for notification?

【讨论】:

  • 你能举个例子吗?当我点击通知时,我知道我需要一个新的活动。但我不知道如何使用该活动关闭应用程序。
  • 您不需要关闭应用程序,这就是重点。你可以试试this,但这太过分了。一旦用户想要运行它,你就强制系统从头开始加载整个应用程序,这违反了 Android 的设计 - 将应用程序保留在内存中并减少加载时间。当您从应用程序移出主屏幕时,该应用程序已经“关闭”并且不使用您的 CPU,单击通知只会“唤醒它”。 TL;DR 不要担心“关闭应用程序”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多