0

I've began working on a simple Android project recently, but it looks like I just cannot get it to work. I have a main Activity (It has @style/Invisible parameter) which checks if my service is running and starts it if it's not. Also, before the check takes place it starts another activity for result. The activity is expected to return the user's username and password to allow the app to login to the system. The problem is that when I install the app on my phone it works fine, but next time I open the app nothing happens. I have to reinstall it.

Here's the main Activity:

   @Override
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

    Intent intent = new Intent(this, LoginGrabber.class);
    startActivityForResult(intent, 100);

    if(isMyServiceRunning()==false) 
    {       
        startService(new Intent(getApplicationContext(), MyService.class));
        Log.i("com.connect","startService");
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if(requestCode == 100){
        if(resultCode == RESULT_OK){
            String username = data.getStringExtra("Username");
            String password = data.getStringExtra("Password");
            //TODO Send to server
            Toast.makeText(this, "Username: " + username + "     Password: " + password, Toast.LENGTH_LONG);
        }
    }
}

private boolean isMyServiceRunning() {
    ActivityManager manager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (MyService.class.getName().equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}

LoginGrabber Activity:

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

public void onSendData(View view){
    Intent intent = new Intent();
    intent.putExtra("Username", ((TextView) findViewById(R.id.email)).getText());
    intent.putExtra("Password", ((TextView) findViewById(R.id.password)).getText());
    setResult(RESULT_OK, intent);
    finish();
}

Here's the Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.test"
android:versionCode="2"
android:versionName="2.0">

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

<supports-screens
android:largeScreens="true"
android:resizeable="true"
android:xlargeScreens="true" />

<uses-permission
android:name="android.permission.INTERNET"
android:required="true" />

<application
android:icon="@drawable/launcher"
android:label="@string/app_name"
android:theme="@style/Invisible">
<activity
android:name="com.connect.Main"
android:excludeFromRecents="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity

<service
android:name="com.connect.MyService"
android:enabled="true"
android:exported="true" />

<activity
android:theme="@style/AppTheme"
android:name="com.connect.LoginGrabber"
android:label="@string/title_activity_login_grabber" />
</application>

</manifest>

What am I doing wrong?

Thanks for your help!

Tadej Gašparovič
  • 160
  • 1
  • 3
  • 12

1 Answers1

0

OK! I found the problem! The problem was that my application was crashing as soon as the service was started because I was testing the app without internet and without handling errors that were thrown when there was no internet available.

Just had to turn WiFi on, that's it!

Tadej Gašparovič
  • 160
  • 1
  • 3
  • 12