Android 在登录页面按钮中注入活动
我正在创建一个登录页面。我创建了一个带有toast“login success”和“login fail”的登录按钮(btnlogin)。但是,我需要在“成功”后使用相同的按钮(btnlogin)启动另一个页面。如何将intent方法注入按钮,使其可以打开另一个活动 主要 短信活动Android 在登录页面按钮中注入活动,android,eclipse,button,android-intent,Android,Eclipse,Button,Android Intent,我正在创建一个登录页面。我创建了一个带有toast“login success”和“login fail”的登录按钮(btnlogin)。但是,我需要在“成功”后使用相同的按钮(btnlogin)启动另一个页面。如何将intent方法注入按钮,使其可以打开另一个活动 主要 短信活动 package com.example.fyp; import com.example.fyp.R; import android.os.Bundle; import android.app.Activity; i
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>
您的问题不太清楚,根据我的理解,您希望在用户成功登录后打开新的活动,然后您应该将代码编写为
if(receiveMessage.equals("success")){
Toast.makeText(getBaseContext(), "Login success!",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, SMS.class);
startActivity(intent);
}
但是请确保您的SMS
类必须在您的清单中注册
试试这个-
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();
}
谢谢你的回答。这就是我在我的maifest文件中所写的@userVee发布您的短信活动谢谢您的回答。你能帮我找出我在日志中遇到的错误吗?”java.lang.RuntimeException:无法启动活动组件信息{com.example.fyp/com.example.fyp.SMS}:java.lang.NullPointerException'是的,我添加了名称。应用程序似乎停止工作。我刚刚发布了清单文件。请从您的SMS活动中删除此文件。在manifest.MAIN中,表示此活动是应用程序的入口点,即启动应用程序时,将创建此活动。
if(receiveMessage.equals("success")){
Toast.makeText(getBaseContext(), "Login success!",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, SMS.class);
startActivity(intent);
}
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();
}