AndroidBeginner
AndroidBeginner

Reputation: 385

Application with Internet for the main and without for the rest

I have an app in which starting page needs internet,

Rest want to work without internet (ie, only one activity need the internet permission).

But when I turn off the Internet, the app shows a message like turn internet connection on and then only I can proceed to further (Here i want to work with out internet). Is there any solution for that?

Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exampleMock.ibps_test"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:screenOrientation="landscape" >

    <activity
        android:name="com.exampleMock.ibps_test.MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="landscape" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.exampleMock.ibps_test.testClass"
        android:label="@string/app_name" 
        android:screenOrientation="landscape"/>

    <activity
        android:name="com.exampleMock.ibps_test.startTest"
        android:label="@string/app_name" 
        android:screenOrientation="landscape"/>
     <activity
        android:name="com.exampleMock.ibps_test.resultActivity"
        android:label="@string/app_name" 
        android:screenOrientation="landscape"/>
     <activity
        android:name="com.exampleMock.ibps_test.showDialog"
        android:label="@string/app_name" 
        android:screenOrientation="landscape"/>

     <activity
        android:name="com.exampleMock.ibps_test.showSolution"
        android:label="@string/app_name"
       android:screenOrientation="landscape" />
     <activity
        android:name="com.exampleMock.ibps_test.InfoGift"
        android:label="@string/app_name"
       android:screenOrientation="landscape"/>
</application>

Main Activity:

public class MainActivity extends ActionBarActivity implements LoaderCallbacks<Void>,                 AsyncHttpRequestDelegate
{
static EditText n;
static EditText p;
ProgressBar pb;
static String mail="";
private DatabaseHelper helper;
private SQLiteDatabase db;
private static WeakReference<MainActivity> mActivity;

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

    helper=new DatabaseHelper(this);
    helper.initializeDataBase();
    db=helper.getWritableDatabase();

    String stat=check();
    if(stat.equals("true"))
    {
        gotoNextPage();
    }
    else
    {
     n=(EditText)findViewById(R.id.name);
     p=(EditText)findViewById(R.id.phone);
     pb=(ProgressBar)findViewById(R.id.progressBar1);
     pb.setVisibility(View.GONE);
     mail=fetchEmail();
      /*    
     if(mail==null)
        {
            EditText m=(EditText)findViewById(R.id.mail);
            m.setVisibility(1);
            mail=m.getText().toString();
        }  */

     Button b=(Button)findViewById(R.id.regBtn);
     b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            if(n.getText().toString().length()<1)
            {
                n.requestFocus();
                Toast.makeText(MainActivity.this, "Enter your  Name", Toast.LENGTH_SHORT).show();
            }
            else if(p.getText().toString().length()<10)
            {
                p.requestFocus();
                Toast.makeText(MainActivity.this, "Enter a valid phone number", Toast.LENGTH_SHORT).show();
            }
            else
            {
                ConnectivityManager connMgr = (ConnectivityManager) 
                        getSystemService(Context.CONNECTIVITY_SERVICE);
                    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
                    if (networkInfo != null && networkInfo.isConnected()) {
                        pb.setVisibility(View.VISIBLE);
                        //call asyncTask
                        startWork();
                    } else {
                        Toast.makeText(MainActivity.this, "No Network connection available...",Toast.LENGTH_SHORT).show();
                    }

            }

        }
    });
  }
}

public String fetchEmail()
{
    String e="";
    Pattern email= Patterns.EMAIL_ADDRESS;
    Account[] accounts= AccountManager.get(this).getAccounts();
    for(Account account:accounts)
    {
        if(email.matcher(account.name).matches())
        {
            e=account.name;
            return e;

        }
    }
    return null;
}

public String check()
{
    String flag="";
    String sql="select * from reg_status";
    Cursor c=db.rawQuery(sql, null);
    if(c!=null)
    {
        c.moveToFirst();
        flag=c.getString(0);
    }
    return flag;
}

public void updateStatus()
{
    String sql="update reg_status set status = 'true'";
    db.execSQL(sql);
    gotoNextPage();
}

public void gotoNextPage()
{
    Intent intent=new Intent(this,startTest.class);
    startActivity(intent);
}

void startWork() {
    getSupportLoaderManager().initLoader(0, (Bundle) null, this);
}

static class AsyncTaskMaker extends AsyncTaskLoader<Void> {
    int progress = 0;
    int percentProgress = 0;
    int fileLength = 0;

    AsyncTaskMaker(MainActivity activity) {
        super(activity);
        mActivity = new WeakReference<MainActivity>(activity);
    }



  @Override
    public Void loadInBackground() {
     System.out.println("inside loadInBackground");
     processWebRequest();

     return null;

    }

}


@Override
public void onLoadFinished(android.support.v4.content.Loader<Void> arg0,
    Void arg1) {
 pb.setVisibility(View.GONE);
 updateStatus();
  //Toast.makeText(MainActivity.this, "Load finished", Toast.LENGTH_SHORT).show();
    gotoNextPage();
}

@Override
public void onLoaderReset(android.support.v4.content.Loader<Void> arg0) {
  //Toast.makeText(MainActivity.this, "Load reset", Toast.LENGTH_SHORT).show();
}


 @Override
 public android.support.v4.content.Loader<Void> onCreateLoader(int arg0, Bundle arg1) {

  AsyncTaskMaker asyncTaskLoader = new AsyncTaskMaker(this);
  asyncTaskLoader.forceLoad();
  return asyncTaskLoader;
}

private static void processWebRequest(){


 HttpClient httpclient = new DefaultHttpClient();
 HttpPost httpPost=new HttpPost("http://beta.wisdom24x7.com/gapps.php/");
 //System.out.println("inside processWebRequest"); 
    try
    {

        List<NameValuePair> pair=new ArrayList<NameValuePair>(4);
        pair.add(new BasicNameValuePair("name",n.getText().toString()));
        pair.add(new BasicNameValuePair("email",mail));
        pair.add(new BasicNameValuePair("phone",p.getText().toString()));
        pair.add(new BasicNameValuePair("exam","AIEEE"));
        httpPost.setEntity(new UrlEncodedFormEntity(pair));

        HttpResponse httpResponse= httpclient.execute(httpPost);
        Log.d("Http Response:", httpResponse.toString());

    }catch(ClientProtocolException e)
    {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }

 }

  @Override
 public void didComplete(HttpRequest request, String responseString) {
  pb.setVisibility(View.GONE);
  //Toast.makeText(MainActivity.this, "data sent", Toast.LENGTH_SHORT).show();

 }

 @Override
 public void didFail(HttpRequest request) {


 }

 @Override
 public void publishProgress(final int progress) {
    if (mActivity.get() != null) {
        mActivity.get().runOnUiThread(new Runnable() {

         @Override
            public void run() {
                mActivity.get().pb.setProgress(progress);
            }
        });
    }
  }
}

Another activity, which does not require internet:

public class showDialog extends ActionBarActivity
{
CheckBox b1,b2,b3,b4;

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

    b1=(CheckBox)findViewById(R.id.checkBox1);
    b2=(CheckBox)findViewById(R.id.checkBox2);
    b3=(CheckBox)findViewById(R.id.checkBox3);
    b4=(CheckBox)findViewById(R.id.checkBox4);

    final List<String> subs=new ArrayList<String>();

    ImageButton bn=(ImageButton)findViewById(R.id.imageButton1);
    bn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            if(b1.isChecked())
                subs.add(b1.getText().toString());
            if(b2.isChecked())
                subs.add(b2.getText().toString());
            if(b3.isChecked())
                subs.add(b3.getText().toString());
            if(b4.isChecked())
                subs.add(b4.getText().toString());
            System.out.print("subjects "+subs);

            Intent intent = new     Intent(showDialog.this,testClass.class);
            intent.putStringArrayListExtra("subject",   (ArrayList<String>) subs);
            startActivity(intent);
        }
    });
}

}

Upvotes: 0

Views: 75

Answers (3)

Muhammed Refaat
Muhammed Refaat

Reputation: 9103

in your AndroidManifest.xml put :

 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

and inonCreate method of each activity you don't want to use the internet_connection in:

WifiManager wifiManager = (WifiManager)this.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);

Upvotes: 0

einschnaehkeee
einschnaehkeee

Reputation: 1888

It seems you didn't write this Android application by yourself (or else you would understand what the message means). This message that "asks for internet connection" is something that is done through your app and not by the Android framework. Please understand your application first, then ask questions about it.

Hint: Search for the String inside your app (by search functionality of your IDE) that is shown in your "asks for internet connection" message and look up why it is displayed. You will see, that you can disable it.

Upvotes: 1

Matt
Matt

Reputation: 1923

You can only set permission for the complete app, not on single activity.

Why is it so important that the user doesn't have an internet connection in the rest of the app? When you don't code anything that connects to the internet in those "internet-free" activities, then you won't use up the (possible) date

Upvotes: 0

Related Questions