无法处理收到的短信
Can't handle received SMS
我想使用 BroadcastReceiver 处理收到的短信。如果有新短信到达,将显示新的 activity。这是我的努力:
public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
StringBuilder sb = new StringBuilder();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdus.length; i++) {
//yes, I need PDU
sb.append(pdus[i]);
}
}
Intent result = new Intent(context, MainActivity.class);
result.putExtra(Mapping.PDU_TEXT, sb.toString());
result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(result);
}
和activity:
public class MainActivity extends Activity {
private TextView mPDUTextView;
private String pduText;
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putString(Mapping.PDU_TEXT, pduText);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
pduText = savedInstanceState.getString(Mapping.PDU_TEXT);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
if (getIntent().getExtras() != null) {
pduText = this.getIntent().getExtras()
.getString(Mapping.PDU_TEXT);
}
}
mPDUTextView = (TextView) this.findViewById(R.id.pduTextView);
mPDUTextView.setText(pduText);
}
}
再看看清单:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.BROADCAST_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="ru.tenet.pdureceiver.SMSReceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
<data android:scheme="sms" />
<data android:port="8998" />
</intent-filter>
</receiver>
<activity
android:name="ru.tenet.pdureceiver.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>
</application>
我已经在真实设备上安装了我的应用程序。如果我发送消息,没有 activity 会 运行。怎么了?
试试这个:
<intent-filter android:priority="1" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
我想使用 BroadcastReceiver 处理收到的短信。如果有新短信到达,将显示新的 activity。这是我的努力:
public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
StringBuilder sb = new StringBuilder();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdus.length; i++) {
//yes, I need PDU
sb.append(pdus[i]);
}
}
Intent result = new Intent(context, MainActivity.class);
result.putExtra(Mapping.PDU_TEXT, sb.toString());
result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(result);
}
和activity:
public class MainActivity extends Activity {
private TextView mPDUTextView;
private String pduText;
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putString(Mapping.PDU_TEXT, pduText);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
pduText = savedInstanceState.getString(Mapping.PDU_TEXT);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
if (getIntent().getExtras() != null) {
pduText = this.getIntent().getExtras()
.getString(Mapping.PDU_TEXT);
}
}
mPDUTextView = (TextView) this.findViewById(R.id.pduTextView);
mPDUTextView.setText(pduText);
}
}
再看看清单:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.BROADCAST_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="ru.tenet.pdureceiver.SMSReceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
<data android:scheme="sms" />
<data android:port="8998" />
</intent-filter>
</receiver>
<activity
android:name="ru.tenet.pdureceiver.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>
</application>
我已经在真实设备上安装了我的应用程序。如果我发送消息,没有 activity 会 运行。怎么了?
试试这个:
<intent-filter android:priority="1" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>