i want to make an application in which i start service which start broadcast reciever for sms_read and every time when new sms come and reciever detact that message. no matter app is running or not.
you don't have to use a Service for Reading and listening for SMS.
All you need to Do is just register a Broadcast receiver in your Application like this .
<receiver android:name=".IncomingSms" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
And just write whatever functionality you have to do upon receiving the SMS in the Broadcast receiver by starting an IntentService or a Thread
Related
I have broadcast receiver to receive intent for new outgoing call as
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
i am getting intent in my receiver from that i open my application to call same phone number up to this all works fine but native dialer app pops up on my application so i want to close native dialer app after opening my activity
See this post: http://android-developers.blogspot.hu/2013/05/handling-phone-call-requests-right-way.html
You will probably need to "setResultData(null);" to tell the system you handled this.
(haven't try)
My app registers in the manifest a broadcastreceiver for PHONE_STATE intent.
<receiver android:name=".receiver.PhoneStateReceiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
<action android:name="android.intent.action.PHONE_STATE"/>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Everything worked fine until i installed another app which handles phone calls (in specific it records audio of phone calls). From that moment my registered receiver does not fire every time. If i remote the second app all returns to be ok.
I assume that this app (as mine) registers a broadcastreceiver for PHONE_STATE intent. Is it possible that this app "consumes" the broadcastreceiver and so it will not fire mine?
i assume that the other broadcast consumer has called the abortBroadcast() method, that ... will prevent any other broadcast receivers from receiving the broadcast.
I'm writing application which handles SMS's and as I plan it should replace stock/default application.
I'm intercepting android.provider.Telephony.SMS_RECEIVED broadcast fired by incoming SMS and publishing my own notification and then calling abortBroadcast(), so in the end there is no notification for incoming new messages which leads to default/stock app.
But the problem is in fact that when user doesn't read for a long enough time (smth like several minutes) incoming SMS stock/default app aroses another broadcast - I suspect just checking that there's unread sms. So user sees 2 notifications: one from default/stock messaging app and another one from mines, which is messing.
I can't find which broadcast fired when there's unread sms?
Any ideas, hints?
You can set the priority of your receiver this way, so you know that it has the top priority and will execute the onReceive() method:
<receiver android:name=".SmsReceiver" android:enabled="true"
android:exported="true" android:priority="999">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
I have created SMS Receiver app... but i want to create it as an service, it should run in the background (i.e no separate UI for this app, want to work like alarm app) and even if mobile restarts it automatically starts... could any one help on this?
My previous SMS Receiver app code was here
Unable to instantiate activity ComponentInfo in Android Receive Sms App
it should run in the background
Your existing BroadcastReceiver for the (undocumented) android.provider.Telephony.SMS_RECEIVED already runs in the background.
and even if mobile restarts it automatically starts
Your existing BroadcastReceiver for the (undocumented) android.provider.Telephony.SMS_RECEIVED already is available after the device reboots.
If you want your service to run at phone startup, you should simply declare a broadcast receiver with this intent filter:
<receiver android:name="MyStartupIntentReceiver">
<intent-filter>
<action
android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
In the broadcast receiver onReceive() method, just launch your service:
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent();
serviceIntent.setAction("myPackage.MyService");
context.startService(serviceIntent);
}
And be sure to link the service in your manifest with the same name of the intent you launch in the broadcast receiver:
<service android:name="MyService">
<intent-filter>
<action
android:name="myPackage.MyService" />
</intent-filter>
</service>
As CommonsWare has said there is no need to actually have a "Service" run in the background for you to receiver SMS broadcasts, if registered in your manifest properly your BroadcastReceiver with "android.provider.Telephony.SMS_RECEIVED" as the intent filter will fire every time an SMS is received no other action required.
Depending on what exactly you want to do, from that broadcast receiver you can then do you work via an actual Service, or probably a better option would to be to use an IntentService. This is because the thread used for the broadcast will get killed shortly after starting, so you should not do any extensive work in it.
It is generally recommend to not use an actual "Service" unless explicitly required....BUT if that is what you need then you need Davide should get you the right direction.
I am working on broadcast receiver and stuck in a problem.
I am receiving a broadcast receiver in Manifest file.
<receiver class=".MyClass" android:name=".MyClass">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.ConnectivityManager.CONNECTIVITY_ACTION" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
this is working fine and it is calling MyClass whenever there is change in connectivity.
Now the problem is whenever my application is not running still this class will receive broadcast receiver. I want it to receive whenever the application is running.
I tried it by extending BroadcastReceiver registering and unregistering broadcast in that class file and it works. But i want to achieve the same by Manifest file.
My problem will solve if it is not receiving anything when application is not opened.
What you are talking is not possible. The whole purpose of having the intent filter in the manifest is being able to receive the intent whether or not your application is running. The only way to do what you want to is by registering/unregistering the receiver from the code [registerReceiver]
The question was asked a long time ago but in case some one landed on this page while searching, It is possible to register and unregister broadcast receiver from code instead of doing that from manifest file. (Checking the Networking Connectivity using BroadcastReceiver in Android)
You said "My problem will solve if it is not receiving anything when application is not opened".
Here how I understand your question and appropriate answer.
android:enabled
Whether or not the broadcast receiver can be instantiated by the system — "true" if it can be, and "false" if not. The default value is "true".
If you want to enable your receiver at runtime, you can set the state to disabled initially. You can do so in the manifest file:
<receiver
android:name=".YourReceiver"
android:enabled="false" >
<!-- your intent filter -->
</receiver>
Source:
http://developer.android.com/guide/topics/manifest/receiver-element.html#enabled
http://www.grokkingandroid.com/enabling-and-disabling-broadcastreceivers/