How to emulate SMS receiving at Android? - android

I have the following code to receive SMS by my application:
public class SMSReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
...
In order to test it, I have to send an SMS with telnet (and I can do nothing with Android x86).
Can I call this onReceive programmatically for ex., when button is pressed or application is started? What kind of parameters should I pass (i.e. where tel number should be? text of the message etc.)?
Upd. I found this answer - https://stackoverflow.com/a/12338541/604388. If I follow that code, i.e.:
intent.setClassName("com.android.mms",
"com.android.mms.transaction.SmsReceiverService");
context.startService(intent);
then standard event is fired (i.e. message is received by standard android Messaging app), but my code at onReceive doesn't work.
If I replace it with:
// intent.setClassName("com.android.mms",
// "com.android.mms.transaction.SmsReceiverService");
context.sendBroadcast(intent);
then my code at onReceive works, but not standard android app.
How can I fix it?

Does this help? DDMS can emulate SMS messages, calles and locations. It's integrated with Eclipse set up for Android development and Android studio. You can find it at:
Window > Open Perspective > Other... > DDMS.
http://developer.android.com/tools/debugging/ddms.html

Related

Background operations when app is killed? (Android API 23+)

To learn something new, I'm developing an Android APP (min SDK version API 23 and Target SDK Version API 28) that allows me and my family to create and share a virtual shopping list through HTTP requests and JSON responses on a free Web. Everything works fine, but I want to add a feature: I would like to get notified when someone makes a change even when the app is killed or has never been launched. I know what the task could do to compare the changes made on the list and I also know that it is something to be done once every 5 minute (for example), but I don't know how to perform background operations when the app is no longer running and it has been killed from the recent tasks list. I gave the Service class a try, but when the app is killed it stops. So I looked for a solution and I found the BroadcastReceiver and made it able to receive a message whenever the Service stops in order to restart it. But from Android API 26 the BroadcastReceiver must be (I guess..) contex-registered.
So this is what I my main Activity does when the onCreate method is called:
ReceiverCall receiver = new ReceiverCall();
registerReceiver(receiver, new IntentFilter("com.dadex.familyapp.startServiceRequest"));
My ReceiverCall which extends the BroadcastReceiver Class:
public class ReceiverCall extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try{
String action = intent.getAction();
if (action.equals("com.dadex.familyapp.startServiceRequest"))
context.startService(new Intent(context, CheckListService.class));
}
catch (Exception e){
Log.e("ERROR", e.getMessage());
}
}
}
And this is my CheckListService onDestroy method:
#Override
public void onDestroy() {
Intent intent = new Intent("com.dadex.familyapp.startServiceRequest");
sendBroadcast(intent);
}
It works fine when the app is launched, but as soon as I kill it, the receiver won't receive anything. So my question is: what is the best way to perform such background operations? Are there other classes I need to learn first? Thanks a lot!
You need a background service, with a notification to keep it alive.
startForegorund()
Search for startForegorund with notification and you will find what you need.

Programmatically check for Android OTA system updates

If you go to Settings->About Phone->Check for updates a check is initiated to see if theres any system updates ready for your phone.
How can I do this action programmatically? Further, I am trying to locate where in the Android source code this happens so I can see it fully and understand it better. Does anyone have any suggestions?
As far as I know, there is no known broadcast, intent or API to do this programmatically.
And it depends on the ROM, and manufacturer.
Sony for example uses a service which, when the wifi is activated, the service checks on Sony's servers for any updates and informs of it.
But when talking about AOSP source, that I do not think happens.
The nearest point of System update is found in packages/apps/Settings/src/com/android/settings/DeviceInfoSettings.java
Protip: grep the string "System update" within the res/values directory and work backwords and find out where that string variable identifier is used!
Edit:
Here's an example broadcast receiver:
public class SystemUpdateClass extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent){
if (intent.getAction().equals("android.settings.SYSTEM_UPDATE_SETTINGS")){
Toast.makeText(context,
"Yup! Received a system update broadcast",
Toast.LENGTH_SHORT).show();
}
}
}
Here's an example code, from within a activity's onCreate:
SystemUpdateClass sysUpdate = new SystemUpdateClass();
IntentFilter filter = new IntentFilter();
filter.addAction("android.settings.SYSTEM_UPDATE_SETTINGS");
registerReceiver(sysUpdate, filter);
Now, what should happen is your app should receive the broadcast, unless I am mistaken that the broadcast is only for system-signed apps... however the rest is left as an exercise :)

How do I check the Currently Visible Activity and fire one of its Methods from Anywhere?

My app has a chat screen to send and receive messages. Whenever a message is received a push notification comes in and shows the latest received message. This all works fine.
But when I get my push notification I would like the chat screen to refresh itself. I guess this would require knowing if the chat screen is currently visible or not. How can I do this from the onReceive() method of my BroadcastReceiver?
Here's a little pseudo code (in my BroadcastReceiver subclass):
public void onReceive(Context context, Intent intent)
{
if(currentlyVisibleActivity.getClass() == chatScreenActivity.class())
{
((chatScreenActivity)currentlyVisibleActivity).getMessages();
}
}
I would like to know how to get the currentlyVisibleActvity and make it call getMessages() if it's a chatScreenActivity.
I assume you are planning the following setup?
C2DM -> YourBroadcastReceiver -> somehow call #getMessages()
How about having the following components:
A BroadcastReceiver <for receiving C2DM push notifications>
An IntentService <for executing #getMessages()>
A ContentProvider <for storing the results of #getMessages()>
An Activity <for displaying the contents of ContentProvider>
Basically, C2DM triggers your receiver which calls #startService, triggering
your IntentService.
Your IntentService calls #getMessages() and stores the messages in your
ContentProvider.
Whenever a change is performed on the data in the ContentProvider you will
trigger ContentResolver#notifyChange().
If your Activity is showing and uses, for instance, a CursorAdapter to read
from the ContentProvider it will automatically refresh.
Sending a chat message would also store a row in the ContentProvider in this scenario - automatically updating the UI just as a received message.

stop android emulator call

I am working on an Android application, having functionality like voicemail.
I am using BroadcastReceiver to get dialing events.
I have to get the event "WHEN CALL IS UNANSWERED (not picked after few rings) FROM RECEIVER".
I will do some actions on caller end against this event.
I am using AVD emulator, and
I do call from one instance to another instance and it calls perfectly,
but the problem is: It continuously calls until I reject or accept the call.
This way I cannot detect that "CALL IS UNANSWERED AFTER A NUMBER OF RINGS".
So I want the Caller emulator to drop the call after a number of rings (if unanswered) like a normal phone.
I can do it (drop the call after some time) by writing some code, but I need the natural functionality of phone in the emulator.
Can anyone please guide me? Is there any settings in the emulator? Or something else?
The code is shown below in case it helps:
public class MyPhoneReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
Bundle extras = intent.getExtras();
if (extras != null)
{
String state = "my call state = " + extras.getString(TelephonyManager.EXTRA_STATE);
Log.w("DEBUG", state);
}
}
Hi i think this is impossible with reference to the link:
http://android.bigresource.com/Track/android-sr1t1eagx/
Regards

android.intent.action.SCREEN_ON doesn't work as a receiver intent filter

I'm trying to get a BroadcastReceiver invoked when the screen is turned on. In my AndroidManifest.xml I have specified :
<receiver android:name="IntentReceiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON"></action>
</intent-filter>
</receiver>
However it seems the receiver is never invoked (breakpoints don't fire, log statements ignored). I've swapped out SCREEN_ON for BOOT_COMPLETED for a test, and this does get invoked.
This is in a 1.6 (SDK level 4) project.
A Google Code Search revealed this, I downloaded the project and synced it, converted it to work with latest tools, but it too is not able to intercept that event.
http://www.google.com/codesearch/p?hl=en#_8L9bayv7qE/trunk/phxandroid-intent-query/AndroidManifest.xml&q=android.intent.action.SCREEN_ON
Is this perhaps no longer supported?
Previously I have been able to intercept this event successfully with a call to Context.registerReceiver() like so
registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// ...
}
}, new IntentFilter(Intent.ACTION_SCREEN_ON));
However this was performed by a long-living Service. Following sage advice from CommonsWare I have elected to try to remove the long-living Service and use different techniques. But I still need to detect the screen off and on events.
Following sage advice from CommonsWare
I have elected to try to remove the
long-living Service and use different
techniques.
Actually, I believe my advice was more of a light blue... :-)
But I still need to detect the screen
off and on events.
There are certain events that Android does not want to start up new processes for, so the device does not get too slow from all sorts of stuff all having to run at once. ACTION_SCREEN_ON is one of those. See this previous question for light blue advice on that topic.
So, you need to ask yourself, "Self, do I really need to get control on those events?". The core Android team would like it if your answer was "no".
This is the best example I've found http://androidexample.com/Screen_Wake_Sleep_Event_Listner_Service_-_Android_Example/index.php?view=article_discription&aid=91&aaid=115
Actullay i was faceing this issue but i resolve it succeessfully
1) start service from your main activity
Intent i = new Intent(MainActivity.this, UpdateService.class);
startService(i);
2) register reciver in service class.
#Override
public void onCreate() {
super.onCreate();
// REGISTER RECEIVER THAT HANDLES SCREEN ON AND SCREEN OFF LOGIC
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReciever();
registerReceiver(mReceiver, filter);
}
3) Done

Categories

Resources