Joke Collection Website - Public benefit messages - Android 2. 1 The broadcast receiver cannot receive data. Why?
Android 2. 1 The broadcast receiver cannot receive data. Why?
& ltuses-permission Android:name = " Android . permission . receive _ SMS " & gt; & lt/uses-permission & gt;
Registered recipient:
& lt recipient android:name= ". GetData " & gt
& lt intention filter & gt
& ltaction Android:name = " Android . provider . telephony . SMS _ RECEIVED "/& gt;
& lt/intent-filter & gt;
& lt/receiver & gt;
Java class GetData:
Import android.content.broadcastreceiver;
Import android.content.context;
Import android.content.intent;
Import android.os.bundle;
Import android.telephony.SMS message;
Import android.widget.toast;
Public class GetData extends BroadcastReceiver {
private static final String MAC tion = " Android . provider . telephony . SMS _ RECEIVED ";
@ Overlay
Public void onReceive (context, intention) (
if(intent.getAction()。 Equal to (mACTION))
{
StringBuilder sb = new StringBuilder();
bundle bundle = intent . get extras();
If (bound! = empty)
{
Object[]obj =(Object[])bundle . get(" PDUs ");
//Create an information object
SMS message[]message = new SMS message[obj . length];
for(int I = 0; I < object length; i++)
{
message[I]= SMS message . createfrompdu((byte[])obj[I]);
}
Used for (SMS current message: message)
{
Sb.append ("received from: \ n");
//The sender's phone number
sb . append(current message . getdisplayoriginatingaddress());
sb . append(" \ n-");
//Information content sent
sb . append(current message . getdisplaymessagebody());
}
}
//Display incoming information with notification.
Toast.makeText(context,sb.toString(),Toast。 Length _ length). show();
//Return to the main program BroadCastTest.
Intent i=new Intent(context,test receiver . class);
//Run a brand-new task
i.addFlags(Intent。 Flag _ Activity _ New _ Task);
context . start activity(I);
}
}
}
Java class: TestReceiver
Import android.app.activity;
Import android.os.bundle;
Import android.widget.textview;
Public class TestReceiver extends Activity {
/* * Called when an activity is first created. */
@ Overlay
public void on create(Bundle saved instancestate){
super . oncreate(savedInstanceState);
setContentView(r . layout . main);
TextView txt =(TextView)findViewById(r . id . TextView 0 1);
Txt.setText ("waiting to receive ...");
}
}
The above code cannot receive the broadcast of SMS received by the system. Has anyone encountered this problem? Answer, thank you.
The reason has been found, so the problem is closed.
Because the foundation and eyes are not good enough
Put < receiver android:name= ". GetData " & gt
& lt intention filter & gt
& ltaction Android:name = " Android . provider . telephony . SMS _ RECEIVED "/& gt;
& lt/intent-filter & gt;
& lt/receiver & gt;
This paragraph is written outside the application label!
I looked for the reason of 1 day and a half, but I couldn't find it. Finally found it, depressed.
- Related articles
- What is caller ID?
- A copy of the double harvest of love career
- How do Apple phones and Android phones transmit data to each other?
- Why don't you go out? Why is the health code yellow? How should the health code turn green?
- New Year's greetings for parents and children
- New Year's greetings 2022 hottest phrases
- General shorthand for Spring Festival greetings (7 1)
- Software that can find virtual short messages
- Good message in the year of the tiger. The more, the better. Thank you!
- What is the general price of the short message group sending platform on the market?