我使用此代码接收短信。当手机收到短信时,我收到一条消息,说明此脚本已停止。有人可以帮忙吗?
ReceivesmsActivity.java
public class ReceivesmsActivity extends Activity {
public TextView info;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receivesms);
info = (TextView) findViewById(R.id.info);
info.setText("Good");
}
public class Receivesms extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
try{
Bundle pudsBundle = intent.getExtras();
Object[] pdus = (Object[]) pudsBundle.get("pdus");
SmsMessage messages =SmsMessage.createFromPdu((byte[]) pdus[0]);
info.setText(messages.getMessageBody());
}catch(Exception e){
Toast.makeText(context,e.toString(),Toast.LENGTH_LONG).show();
}
}
这是我接收短信的清单文件:
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<receiver android:name=".ReceivesmsActivity">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
答案 0 :(得分:0)
我建议你按照代码的方式采用一种方式,
public class ReceivesmsActivity extends Activity
{
private static TextView info;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receivesms);
info = (TextView) findViewById(R.id.info);
info.setText("Good");
}
public void setInfoText ( String str )
{
info.setText ( str );
}
}
public class Receivesms extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
try
{
Bundle pudsBundle = intent.getExtras();
Object[] pdus = (Object[]) pudsBundle.get("pdus");
SmsMessage messages =SmsMessage.createFromPdu((byte[]) pdus[0]);
//info.setText(messages.getMessageBody());
ReceivesmsActivity.setInfoText ( messages.getMessageBody().toString() );
}
catch(Exception e)
{
Toast.makeText(context,e.toString(),Toast.LENGTH_LONG).show();
}
}
}