如何在无线设置中添加return = true

时间:2013-11-22 04:09:39

标签: android

我正在开发一个项目而且卡住了,我想要的是启用wifi或互联网后,后退按钮(硬件按钮)应该让我回到SplitScreen.xml而不是关闭应用程序请不要给任何代码添加,因为我的代码工作正常,insted,纠正我的代码

}}

1 个答案:

答案 0 :(得分:1)

要实现您想要的行为,请将您的呼叫移至打开无线设置到MainActivity,并向其Intent添加一个布尔变量,以指示是否应在MainActivity.onCreate中打开设置。

Total Redo Edit

这是你的SplashActivity.java。您应该遇到的唯一问题是布局名称。我不记得你的名字。

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog.OnClickListener;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class SplashScreen extends Activity
{
    static ConnectivityManager cm;
    AlertDialog dailog;
    AlertDialog.Builder build;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);// checking
        build = new AlertDialog.Builder(this);
        setContentView(R.layout.activity_splash);
        // if connection is
        // there screen goes
        // to next screen
        // else shows
        // message
        if (cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting()
            || cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting())
        {
            Log.e("cm value", "" + cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
                  .isConnectedOrConnecting());
            Toast.makeText(this, "Internet is active", 2000).show();
            Thread mythread = new Thread() {
                public void run()
                {
                    try
                    {
                        sleep(5000);
                    }
                    catch (Exception e)
                    {
                    }
                    finally
                    {
                        Intent intent = new Intent(SplashScreen.this,
                                                   MainActivity.class);
                        startActivity(intent);

                        finish();
                    }
                }
            };
            mythread.start();
        }
        else
        {
            build.setMessage("This application requires Internet connection. Would you connect to internet ?");
            build.setPositiveButton("Yes", new OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        // TODO Auto-generated method stub
                        Intent intent = new Intent(SplashScreen.this,
                                                   MainActivity.class);
                        intent.putExtra("showSettings", true);
                        startActivity(intent);

                        finish();
                        //startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
                    }
                });
            build.setNegativeButton("No", new OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        // TODO Auto-generated method stub
                        build.setMessage("Are sure you want to exit?");
                        build.setPositiveButton("Yes", new OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog, int which)
                                {
                                    // TODO Auto-generated method stub
                                    finish();
                                }
                            });
                        build.setNegativeButton("NO", new OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog, int which)
                                {
                                    // TODO Auto-generated method stub
                                    finish();
                                    Intent intent = new Intent(SplashScreen.this,
                                                               SplashScreen.class);
                                    startActivity(intent);

                                    dialog.dismiss();

                                }
                            });
                        dailog = build.create();
                        dailog.show();
                    }
                });
            dailog = build.create();
            dailog.show();
        }
    }
}

这是你的MainActivity。

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.widget.Toast;

public class MainActivity extends Activity
{
    ToggleButton toggle;

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

        toggle = (ToggleButton)findViewById(R.id.tglbtn1);
        toggle.setOnClickListener(new OnClickListener()
            {
                public void onClick(View v)
                {
                    if (toggle.isChecked())
                    {
                        Toast.makeText(getApplicationContext(), "Boosting For Next 60 Minutes, Minimize the Application", Toast.LENGTH_LONG).show();
                    }
                    else
                    {
                        Toast.makeText(getApplicationContext(), "Boosting Turned Off",    Toast.LENGTH_LONG).show();
                    }
                }
            }
        );

        Intent intent = getIntent();
        boolean show = intent.getBooleanExtra("showSettings", false);

        if (show)
        {
            startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
        }
    }
}

修复SplashScreen中的布局名称问题,你应该做得很好。我刚刚运行了这段代码,它按预期执行。