处理程序延迟不考虑android

时间:2014-04-28 08:18:06

标签: android android-service android-handler

我使用Handler在我的应用中有延迟,但似乎不起作用。整个过程都在服务中,因为我需要在屏幕关闭时进行检查。在我的MainActivity中,我使用开关获取这部分代码:

//attach a listener to check for changes in state
        mySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                                             boolean isChecked) {

                    if(isChecked){
                        LayoutInflater l = LayoutInflater.from(MainActivity.this);
                        View dview = l.inflate(R.layout.dialog, null);
                        AlertDialog.Builder ad = new AlertDialog.Builder(MainActivity.this);
                        ad.setView(dview);
                        final EditText edit = (EditText) dview.findViewById(R.id.edit);

                        ad.setCancelable(false)
                            .setTitle("Set minutes")
                            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id){
                                    String e = edit.getText().toString();
                                    if(e!=null && !e.isEmpty()){
                                        try{
                                            n = Integer.parseInt(e);
                                        } catch (NumberFormatException ex) {
                                            ex.printStackTrace();
                                            n=0;
                                        }
                                    }   
                                    startService(new Intent(MainActivity.this, AlarmReceiver.class));
                                }
                            })
                            .setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id){

                                    dialog.cancel();
                                }
                            });
                        AlertDialog alertD = ad.create();

                        alertD.show();


                        SharedPreferences.Editor editor = getSharedPreferences("com.twixkat.twixconnection", MODE_PRIVATE).edit();
                        editor.putBoolean("switchstate", true);
                        editor.commit();
                    }else{
                        Toast.makeText(MainActivity.this, "Service Stopped ...", Toast.LENGTH_SHORT).show();
                        stopService(new Intent(MainActivity.this, AlarmReceiver.class));
                        SharedPreferences.Editor editor = getSharedPreferences("com.twixkat.twixconnection", MODE_PRIVATE).edit();
                        editor.putBoolean("switchstate", false);
                        editor.commit();
                    }

                }
            });

这只是创建一个对话框,询问必须通过多少分钟。所以我可以手动设置延迟,从对话框中的edittext中获取值。 (就我而言n)。之后,我在服务中有真正的代码:

@Override
    public void onStart(Intent intent, int startId) {
        MainActivity m = new MainActivity();
        // For time consuming an long tasks you can launch a new thread here...
        Toast.makeText(this, " Service partito", Toast.LENGTH_LONG).show();
        Handler handler = new Handler();
        handler.postDelayed(new Runnable(){
                @Override
                public void run(){
                    screen();
                }
            }, (m.n / 1000) / 60);
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "Service destroyed", Toast.LENGTH_LONG).show();

    }

    @Override
    public void screen(){
        IntentFilter intent = new IntentFilter(Intent.ACTION_SCREEN_ON);
        intent.addAction(Intent.ACTION_SCREEN_OFF);
        wifimanager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
        registerReceiver(new BroadcastReceiver(){
                @Override
                public void onReceive(Context context, Intent i){
                    if (i.getAction().equals(Intent.ACTION_SCREEN_OFF)){
                        Toast.makeText(AlarmReceiver.this, "Screen off, wifi off ...", Toast.LENGTH_SHORT).show();
                        wifimanager.setWifiEnabled(false);

                    } else if (i.getAction().equals(Intent.ACTION_SCREEN_ON)){
                        Toast.makeText(AlarmReceiver.this, "Screen on...", Toast.LENGTH_SHORT).show();
                    }
                }
            }, intent);
    }

其中m.n是edittext的值,因为我无法在服务中创建对话框。问题是screen()方法在屏幕关闭时开始,没有等待延迟。实际上我无法找到解决问题的其他方法..

1 个答案:

答案 0 :(得分:1)

您必须将毫秒传递给postDelayed方法,因此您应该将m.n值乘以更改

(m.n / 1000) / 60

(m.n * 1000) * 60

将为您提供用户输入的分钟数(毫秒)