Gefunden bei: http://www.anddev.org
Here on request a simple Login that uses your Google Account
Main.java
Java
package de.demo.main;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import de.demo.login.Login;
public class Main extends Activity {
private TextView tv;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
startActivityForResult(new Intent(Main.this, Login.class), 1);
tv = new TextView(this);
setContentView(tv);
}
private void startup(Intent i) {
boolean success = i.getBooleanExtra("userid",false);
if(success)
tv.setText("Login accepted");
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 1 && resultCode == RESULT_CANCELED)
finish();
else
startup(data);
}
}
Alles anzeigen
Login.java
Java
package de.demo.login;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import de.demo.main.R;
public class Login extends Activity {
private static final String UPDATE_URL = "https://www.google.com/accounts/LoginAuth";
public ProgressDialog progressDialog;
private EditText UserEditText;
private EditText PassEditText;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.GoogleLogin);
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Please wait...");
progressDialog.setIndeterminate(true);
progressDialog.setCancelable(false);
UserEditText = (EditText) findViewById(R.id.username);
PassEditText = (EditText) findViewById(R.id.password);
Button button = (Button) findViewById(R.id.okbutton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int usersize = UserEditText.getText().length();
int passsize = PassEditText.getText().length();
if(usersize > 0 && passsize > 0) {
progressDialog.show();
String user = UserEditText.getText().toString();
String pass = PassEditText.getText().toString();
doLogin(user, pass);
} else createDialog("Error","Please enter Username and Password");
}
});
button = (Button) findViewById(R.id.cancelbutton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { quit(false, null); }
});
}
private void quit(boolean success, Intent i) {
setResult( (success) ? -1:0, i);
finish();
}
private void createDialog(String title, String text) {
AlertDialog ad = new AlertDialog.Builder(this)
.setPositiveButton("Ok", null)
.setTitle(title)
.setMessage(text)
.create();
ad.show();
}
private void doLogin(final String login, final String pass) {
Thread t = new Thread() {
public void run() {
Looper.prepare();
DefaultHttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 15000);
HttpResponse response;
HttpEntity entity;
try {
HttpGet get = new HttpGet(UPDATE_URL+"?Email="+login+"&Passwd="+pass);
response = client.execute(get);
entity = response.getEntity();
InputStream is = entity.getContent();
read(is);
is.close();
if (entity != null) entity.consumeContent();
} catch (Exception e) {
progressDialog.dismiss();
createDialog("Error", "Couldn't establish a connection");
}
Looper.loop();
}
};
t.start();
}
private void read(InputStream in) {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp;
try {
sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
LoginContentHandler uch = new LoginContentHandler();
xr.setContentHandler(uch);
xr.parse(new InputSource(in));
} catch (ParserConfigurationException e) {
} catch (SAXException e) {
} catch (IOException e) {}
}
private class LoginContentHandler extends DefaultHandler {
private boolean in_title = false;
private boolean success;
public void startElement(String n, String l, String q, Attributes a)
throws SAXException
{
if(l == "title") in_title = true;
}
public void endElement(String n, String l, String q) throws SAXException {
if(l == "title") {
in_title = false;
progressDialog.dismiss();
if(success) {
Intent i = new Intent();
i.putExtra("success", success);
quit(true,i);
} else {
createDialog("Error", "Invalid username and/or password");
success = false;
}
}
}
public void characters(char ch[], int start, int length) {
if(in_title) {
String erg = new String(ch, start, length);
success = (erg.equals("Redirecting")) ? true : false;
}
}
public void startDocument() throws SAXException { }
public void endDocument() throws SAXException { }
}
}
Alles anzeigen
GoogleLogin.xml
Java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Username"
/>
<EditText
android:id="@+id/username"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:fadingEdge="horizontal"
android:layout_marginBottom="20dip"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Password"
/>
<EditText
android:id="@+id/password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true"
android:singleLine="true"
android:fadingEdge="horizontal"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom">
<Button
android:id="@+id/okbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Login"
/>
<Button
android:id="@+id/cancelbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Cancel"
/>
</LinearLayout>
</LinearLayout>
Alles anzeigen
AndroidManifest.xml
Java
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.demo.main"
android:versionCode="1"
android:versionName="1.0.0">
<application android:icon="@drawable/icon" android:label="LoginDemo">
<activity android:name="de.demo.main.Main" android:label="LoginDemo">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="de.demo.login.Login" android:label="LoginDemo">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
Alles anzeigen
No mysql db or php script needed.. just use your google account login.
I hope i understand the request correct and this is the right solution Smile
I should mention that this is not a rock stable login method.. if google
change their login site this no longer works.
And by the way.. in case you wonder.. google sends wrong content-length thats
the reason for this hack like method instead of a clean check etc.