我要构建的Android应用是短信投票应用。 为了解释我的项目,
我的MainActivity应该从RedirectActivity接收SMS数据,并识别它是1还是2,然后递增变量numOf1
或numOf2
以及total
。
public class MainActivity extends AppCompatActivity {
private int numOf1 = 0;
private int numOf2 = 0;
private int sum = 0;
private TextView number1 ;
private TextView number2 ;
private TextView total;
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
String data= getIntent().getStringExtra("text");
if(data.equals("1")){
numOf1++;
number1.setText(Integer.toString(numOf1));
}
else if(data.equals("2")){
numOf2++;
number2.setText(Integer.toString(numOf2));
}
sum = numOf1 + numOf2;
total.setText(Integer.toString(sum));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number1 = (TextView)findViewById(R.id.number1);
number2 = (TextView)findViewById(R.id.number2);
total = (TextView)findViewById(R.id.total);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.set:
startActivity(new Intent(MainActivity.this, setting.class));
break;
}
return true;
}
}
RedirectActivity将通过SMSReceiver类的Intent接收SMS数据。
public class RedirectActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String text = intent.getStringExtra("text");
Intent i = new Intent(this, MainActivity.class);
i.putExtra("text", text);
startActivity(i);
}
}
SMSReceiver类
public class SMSReceiver extends BroadcastReceiver {
static final String logTag = "SmsReceiver";
static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += msgs[i].getMessageBody().toString();
}
Intent i=new Intent(context,RedirectActivity.class);
i.putExtra("text", str);
context.startActivity(i);
}
}
}
我遇到的问题是,每当我通过localhost 5554发送带有1或2的消息并接收它时,我的应用程序根本不算数。
我该如何解决? :d
答案 0 :(得分:0)
我建议将onNewIntent()
中的MainActivity
内的代码移至onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number1 = (TextView)findViewById(R.id.number1);
number2 = (TextView)findViewById(R.id.number2);
total = (TextView)findViewById(R.id.total);
String data= getIntent().getStringExtra("text");
if(data.equals("1")){
numOf1++;
number1.setText(Integer.toString(numOf1));
}
else if(data.equals("2")){
numOf2++;
number2.setText(Integer.toString(numOf2));
}
sum = numOf1 + numOf2;
total.setText(Integer.toString(sum));
}
但是onNewIntent
中的代码无效,因为您没有调用setIntent(intent)
。在致电setIntent(intent)
之前,您应致电getInent()
。
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
...
}
有关onNewIntent()
Here
我希望这会有所帮助。