Android 未显示简单SMS广播接收器的Toast
我是一名Android初学者,我编写了以下代码,这些代码可以用API 10在手机中显示toast,但我不能用API 19在手机中显示toast并运行onReceive 我在互联网上搜索了一下,发现我应该用flag\u include\u stopped\u packages在intent上添加flag。我想这就是我问题的答案 但是我如何为系统广播添加它呢?如果有人能展示合适的代码,我将不胜感激。我在互联网上找不到任何合适的代码来展示这个。谢谢大家! SMS.javaAndroid 未显示简单SMS广播接收器的Toast,android,android-toast,android-broadcastreceiver,Android,Android Toast,Android Broadcastreceiver,我是一名Android初学者,我编写了以下代码,这些代码可以用API 10在手机中显示toast,但我不能用API 19在手机中显示toast并运行onReceive 我在互联网上搜索了一下,发现我应该用flag\u include\u stopped\u packages在intent上添加flag。我想这就是我问题的答案 但是我如何为系统广播添加它呢?如果有人能展示合适的代码,我将不胜感激。我在互联网上找不到任何合适的代码来展示这个。谢谢大家! SMS.java public class S
public class SMS extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
public class IncomingSms extends BroadcastReceiver {
final SmsManager sms = SmsManager.getDefault();
public void onReceive(Context context,Intent intent) {
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String message = currentMessage.getDisplayMessageBody();
String senderNum = currentMessage.getDisplayOriginatingAddress();
Log.i("SmsReceiver", senderNum + message);
Toast.makeText(context,
"send from " + senderNum + message, Toast.LENGTH_LONG).show();
}
}
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" + e);
}
}
}
IncomingSms.java
public class SMS extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
public class IncomingSms extends BroadcastReceiver {
final SmsManager sms = SmsManager.getDefault();
public void onReceive(Context context,Intent intent) {
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String message = currentMessage.getDisplayMessageBody();
String senderNum = currentMessage.getDisplayOriginatingAddress();
Log.i("SmsReceiver", senderNum + message);
Toast.makeText(context,
"send from " + senderNum + message, Toast.LENGTH_LONG).show();
}
}
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" + e);
}
}
}
public class IncomingSms扩展广播接收器{
final smsmsmanager sms=smsmsmanager.getDefault();
公共void onReceive(上下文、意图){
final Bundle=intent.getExtras();
试一试{
if(bundle!=null){
最终对象[]pdusObj=(对象[])bundle.get(“pdus”);
对于(int i=0;i
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.security.security" >
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<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" />
<category android:name="android.intent.category.INFO"/>
</intent-filter>
</activity>
<receiver android:name=".IncomingSms">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
首先,通过扩展Android默认
BroadcastReceiver
类来创建自己的Receiver类。然后@重写类中的onReceive()
方法。当手机收到新短信时,将调用此onReceive()
方法。下面是我的Receiver类,用于获取传入的SMS通知
//Here is your broadcast receiver class
public class YourBroadcastReceiver extends BroadcastReceiver{
private static final String TAG = "MyBroadCastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG,"OnReceive ++>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
Bundle bndl = intent.getExtras();
SmsMessage[] msg = null;
String str = "";
if (null != bndl)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bndl.get("pdus");
msg = new SmsMessage[pdus.length];
for (int i=0; i<msg.length; i++){
msg[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS From " + msg[i].getOriginatingAddress();
str += " :\r\n";
str += msg[i].getMessageBody().toString();
str += "\n";
}
//---display incoming SMS as a Android Toast---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
}
//这是您的广播接收器类
公共类YourBroadcastReceiver扩展了BroadcastReceiver{
私有静态最终字符串标记=“MyBroadCastReceiver”;
@凌驾
公共void onReceive(上下文、意图){
Log.i(标记“OnReceive++>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>”;
Bundle bndl=intent.getExtras();
SmsMessage[]消息=null;
字符串str=“”;
如果(null!=bndl)
{
//---检索收到的SMS消息---
Object[]pdus=(Object[])bndl.get(“pdus”);
msg=新SmsMessage[pdus.length];
对于(int i=0;i