在登录页面按钮中注入活动

时间:2013-12-29 10:00:44

标签: android eclipse button android-intent

我正在创建一个登录页面。我创建了一个登录按钮(btnlogin),其中包含toast“登录成功”和“登录失败”。但是我需要在“成功”之后使用相同的按钮(btnlogin)启动另一个页面。如何将intent方法注入按钮,以便它可以打开另一个活动?

主要

package com.example.fyp;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

EditText txtUserName;
EditText txtPassword;
Button btnLogin;

String username, password;

HttpClient httpclient;
HttpPost httppost;
HttpResponse response;
HttpEntity entity;

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

    txtUserName = (EditText) this.findViewById(R.id.txtUname);
    txtPassword = (EditText) this.findViewById(R.id.txtPwd);
    btnLogin = (Button) this.findViewById(R.id.btnLogin);
    btnLogin.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    httpclient = new DefaultHttpClient();
    httppost = new HttpPost("http://10.0.2.2/New/index.php");

    username = txtUserName.getText().toString();
    password = txtPassword.getText().toString();

    try {

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("username", username));
        nameValuePairs.add(new BasicNameValuePair("password", password));

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        response = httpclient.execute(httppost);
        StringBuilder builder = new StringBuilder();
        if (response.getStatusLine().getStatusCode() == 200) {
            entity = response.getEntity();

            if (entity != null) {
                InputStream instream = entity.getContent();

    BufferedReader reader = new BufferedReader(new  InputStreamReader(instream));
                String line;
                while ((line = reader.readLine()) != null) {
                  builder.append(line);
                }

                String receiveMessage = builder.toString();
                if(receiveMessage.equals("success")){
                    Toast.makeText(getBaseContext(), "Login success!",
                            Toast.LENGTH_SHORT).show();

                }else if(receiveMessage.equals("fail")){
                    Toast.makeText(getBaseContext(), "Login fail!",
                            Toast.LENGTH_SHORT).show();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();

        Toast.makeText(getBaseContext(), "Login Unsuccessful",
                Toast.LENGTH_SHORT).show();

    }
}



}

开始另一项活动(SMS)

btnlogin.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
         Intent intent = new Intent(v.getContext(), SMS.class);
         startActivityForResults(intent, 0);
 });

短信活动

package com.example.fyp;

import com.example.fyp.R;
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SMS extends Activity {
Button sendSMS;
EditText msgTxt;
EditText numTxt;
IntentFilter intentFilter;

private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent)
    {
        TextView inTxt = (TextView) findViewById(R.id.textMsg);
        inTxt.setText(intent.getExtras().getString("sms"));
    }
};
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    intentFilter = new IntentFilter();
    intentFilter.addAction("SMS_RECEIVED_ACTION");

    sendSMS = (Button)findViewById(R.id.sendBtn);
    msgTxt = (EditText)findViewById(R.id.message);
    numTxt = (EditText)findViewById(R.id.numberTxt);
    sendSMS.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String myMsg = msgTxt.getText().toString();
            String theNumber = numTxt.getText().toString();
            sendMsg(theNumber, myMsg);

        }
    });
}

protected void sendMsg(String theNumber, String myMsg) {
    // TODO Auto-generated method stub
    String SENT = "Message Sent";
    String DELIVERED = "Message Delivered";

PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);

registerReceiver(new BroadcastReceiver()
{
    public void onReceive(Context arg0, Intent arg1)
    {
        switch(getResultCode())
        {
        case Activity.RESULT_OK:
        Toast.makeText(getBaseContext(), "SMS sent", Toast.LENGTH_LONG).show();
            break;
        case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
    Toast.makeText(getBaseContext(), "Generic Failure",   Toast.LENGTH_LONG).show();
            break;
        case SmsManager.RESULT_ERROR_NO_SERVICE:
    Toast.makeText(getBaseContext(), "No Service", Toast.LENGTH_LONG).show();
            break;      
        }
    }
}, new IntentFilter(SENT));

registerReceiver(new BroadcastReceiver()
{
    public void onReceive(Context arg0, Intent arg1)
    {
        switch(getResultCode())
        {
        case Activity.RESULT_OK:
    Toast.makeText(getBaseContext(), "SMS delivered", Toast.LENGTH_LONG).show();
            break;
        case Activity.RESULT_CANCELED:
    Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_LONG).show();
            break;  
        }
    }
}, new IntentFilter(DELIVERED));

SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(theNumber, null, myMsg, sentPI, deliveredPI);  
}

protected void onResume(){
registerReceiver(intentReceiver, intentFilter);
super.onResume();
}
protected void onPause(){
unregisterReceiver(intentReceiver);
super.onPause();    
}   
}

清单文件

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

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

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />

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

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity  android:name=".SMS"
               android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <receiver android:name=".SMSReceiver" >
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
    <activity
        android:name="com.example.fyp.Main"
        android:label="@string/title_activity_main" >
    </activity>
</application>

</manifest>

2 个答案:

答案 0 :(得分:0)

试试这个 -

String receiveMessage = builder.toString();
if(receiveMessage.equals("success")){
Toast.makeText(getBaseContext(), "Login success!",Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this, SMS.class);
finish();
}

答案 1 :(得分:-1)

你的问题不是那么清楚,根据我的理解,你想在用户成功登录时打开新活动,那么你应该编写代码

if(receiveMessage.equals("success")){
                    Toast.makeText(getBaseContext(), "Login success!",
                            Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, SMS.class);
         startActivity(intent);
}

但请确保您必须在SMS

中注册Manifest课程