Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么当我点击复选框时,通知会在10秒后出现?_Android_Sharedpreferences_Android Preferences_Android Notifications_Android Checkbox - Fatal编程技术网

Android 为什么当我点击复选框时,通知会在10秒后出现?

Android 为什么当我点击复选框时,通知会在10秒后出现?,android,sharedpreferences,android-preferences,android-notifications,android-checkbox,Android,Sharedpreferences,Android Preferences,Android Notifications,Android Checkbox,您好,我的代码显示一个关于电池电量的通知有一个小问题。我已将首选项的代码插入到onReceive中,如下所示: private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() { @SuppressLint("NewApi") @SuppressWarnings("deprecation") @Override public void onReceive(Context context

您好,我的代码显示一个关于电池电量的通知有一个小问题。我已将首选项的代码插入到
onReceive
中,如下所示:

private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() {
    @SuppressLint("NewApi")
    @SuppressWarnings("deprecation")
    @Override
    public void onReceive(Context context, Intent intent) { 
        // prefer
        setPref.setOnClickListener(new Button.OnClickListener(){    
            @Override    
            public void onClick(View arg0) {     
                // TODO Auto-generated method stub   
                Intent intent = new Intent(MainActivity.this,settings.class);
                startActivityForResult(intent, 0);
            }});         
        checkPref();
    }

    @SuppressLint("NewApi")
    private void checkPref() {
        SharedPreferences myPref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
        boolean pref_opt1 = myPref.getBoolean("pref_opt1", false); 
        int level= intent.getIntExtra(BatteryManager.EXTRA_LEVEL,-1);

        if (pref_opt1) {
            NotificationManager notifi = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notification = new Notification.Builder(getApplicationContext())
                    .setContentTitle("Battery Informations")
                    .setContentText("Batteria al"+" "+level+"%")
                    .setSmallIcon(R.drawable.icon_small_not)
                    .setTicker(level+"%")
                    .build();

            notification.flags = Notification.FLAG_ONGOING_EVENT;
            Intent i = new Intent(context, MainActivity.class); 
            PendingIntent penInt = PendingIntent.getActivity(getApplicationContext(), 0 , i , 0);
            notifi.notify(215,notification);
        } else {
            NotificationManager notifi = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
            notifi.cancel(215);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        checkPref();
    }
}

一切进展顺利,但当我选中该框时,通知会在大约10秒后出现。为什么?

每次接收广播时,您都要检查复选框是否已选中(在checkPref()中),所发生的事情是您选中复选框,然后接收广播并发布通知。您希望它做什么?

我应该补充一点,在代码中包含所有这些注释不是一个好的做法。你有什么特别的原因吗,或者你只是在使用从某处获得的代码,做eclipse希望你做的事情,让黄色的小曲线消失,我想要的是,如果首选项中的复选框被选中,它只显示电池电量通知。这是错误的方法吗?您希望它在您选中该复选框后将电池电量添加到通知栏中(立即,而不是稍后),对吗?是的,当我选中该复选框时,通知栏中将显示电池电量通知。如果不加以制止,显然就会消失。但必须是即时的。