In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you the example analysis of SMS trigger in Android development, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
BroadCast Receiver in Android can be used to listen for broadcast events. Before you can use Broadcast, you must use code or register with AndroidManifest.xml.
The following example implementation uses SMS to trigger the example in AndroidGraphics2DTutorial. The format of the SMS message is @ demo:xxxx, and xxxx is the example name. For example, if you start the Colors example, send it to the phone: @ demo:Colors. After receiving the SMS message, the phone first checks whether the format of the SMS message conforms to @ demo:xxxx. If so, this launches the corresponding example.
Add a custom Broadcast Receiver SmsMessageReceiver to AndroidGraphics2DTutorial to monitor received SMS messages:
Public class SmsMessageReceiver extends BroadcastReceiver {private static final String queryString= "@ demo:"; @ Override public void onReceive (Context context, Intent intent) {Bundle extras = intent.getExtras (); if (extras = = null) return; Object [] pdus = (Object []) extras.get ("pdus"); for (int I = 0; I < pdus.length) SmsMessage message +) {SmsMessage message = SmsMessage.createFromPdu ((byte []) pdus [I]); String fromAddress = message.getOriginatingAddress (); String fromDisplayName = fromAddress; String msg=message.getMessageBody () If (msg.startsWith (queryString)) {/ / Trigger the main activity to fire up a dialog / / that shows/reads the received messages Intent di = new Intent (); di.setClass (context, AndroidGraphics2DTutorial.class) Di.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); di.putExtra (AndroidGraphics2DTutorial.SMS_FROM_ADDRESS_EXTRA, fromAddress); di.putExtra (AndroidGraphics2DTutorial.SMS_FROM_DISPLAY_NAME_EXTRA, fromDisplayName) Di.putExtra (AndroidGraphics2DTutorial.SMS_MESSAGE_EXTRA, msg); context.startActivity (di);}
OnReceive will be executed when the Broadcast event occurs. Here, the content of the SMS message is detected. If it starts with @ demo:, the AndroidGraphics2DTutorial Main Activity is launched.
Modify AndroidMainifest.xml
At the same time, add permission, which is similar to Java ME Android. Some API needs to specify the corresponding Permission before it can be used.
Modify AndroidGraphics2DTutorial.java to process SMS messages:
Public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); Resources res = getResources (); String [] activity_Names = res.getStringArray (R.array.activity_name); String [] activity_Infos = res.getStringArray (R.array.activity_info); for (int item0) {String demoName=mMessage.substring (index+queryString.length ()); Intent intent = new Intent () Intent.setClassName (this, packgeName+ ".example." + demoName); startActivity (intent);}
Here's a test: if you use a device, send @ demo:Colors.
If you use an emulator, you can start two emulators:
The number 5554 in the upper left corner of the simulator is the number of the simulator. If @ demo:Paths is sent, the Paths example is automatically triggered. If the AndroidGraphics2DTutorial is not running, the application will be launched automatically when the phone receives the SMS message.
The above is a sample analysis of SMS trigger in Android development. have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.