Receiving sms through a Service Class - android

I wish to make a service class that receive sms received from a broadcast receiver and get database updated for that I hava developed some code for SmsService class but it does not work. Sir pl tell me is it possible to receive SMS through a service class and update database at background. thanks and sorry for my bad pronounciation if not understand.
below is my source code of SmsService Class.
public class SmsService extends Service {
private SMSReceiver mSMSreceiver;
private IntentFilter mIntentFilter;
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public SmsService(){
/*dba = new DataBaseAdapter(this);*/
mSMSreceiver = new SMSReceiver(this);
}
#Override
public void onCreate(){
super.onCreate();
mIntentFilter = new IntentFilter();
mIntentFilter.addAction(ConstantClass.SMS_RECEIVED);
registerReceiver(mSMSreceiver,mIntentFilter);
}
#Override
public int onStartCommand(Intent intent , int flags, int type){
return START_STICKY;
}
#Override
public void onDestroy(){
super.onDestroy();
//unregisterReceiver(mSMSreceiver);
}
}
thanks in advance
Om Parkash Kaushik

You can check
http://mobiforge.com/developing/story/sms-messaging-android
This tutorial shows how to send and then receives, giving a notification (toast) when you receive a text.
Let me know if this helps!
Also you can check android: register application to receive sms for more details.
Thanks

Related

SCREEN_OFF Broadcast Receiver not firing inside of Service in separate process

Here is my stuff:
public class NotificationSystem extends Service implements Runnable {
private final Thread worker = new Thread(this);
private boolean alreadyRunning = false;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
IntentFilter filter = new IntentFilter();
ScreenManager screenManager = new ScreenManager();
filter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(screenManager, filter);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!alreadyRunning) {
worker.start();
this.alreadyRunning = true;
}
return START_STICKY;
}
#Override
public void run() {
while(true) {
System.out.println("Thread doing stuff");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private static class ScreenManager extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
System.out.println("Screen is off");
}
}
}
The broadcast receiver never fires when the screen toggles off, why?
Is there some problem if there is that broadcast receiver inside of a service? Do I need anything in the manifest for SCREEN_OFF to toggle?
Is it another magical decision that the Android core team did instead of me? Maybe firing a broadcast receiver drains battery, ey?
I didn't got your question much but I got the answer for permission question of yours.
For working with Broadcast receivers you are always require to give a permission to AndroidManifest.xml which allows you to listen to state changes in your receiver. Also Register your receiver in your manifest file.
which is added by:
<uses-permission android:name="android.permission.READ_PHONE_STATE" >
I hope this helps !!
It is a firmware bug.
It's not working on my phone, but it works fine in the emulator.
Go figure.

Open app by detecting device power button press twice in android

I am trying to build an app which can be also opened by pressing device power button twice. I have followed this question's answer to build my app. But the background service is not working when the application is closed. Though it seems to work fine when the application is running. Here is the code for service LockService.java
public class LockService extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);
final BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
return super.onStartCommand(intent, flags, startId);
}
public class LocalBinder extends Binder {
LockService getService() {
return LockService.this;
}
}}
BroadcastReceiver class for application ScreenReceiver.java
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(getApplicationContext(), LockService.class));
}}
Finally AndroidManifest.xml file is here.
What should i do to work the app properly?
When your application is closed, LockService gets destroyed. That is why it doesn't work. What you can do is in your Main Activity's onDestroy method, stop the service. Further, in onDestroy method of LockService, send an intent to a broadcast receiver which will start your service again. In this way, whenever your app is closed, broadcast receiver will start your service again and the code inside the service will then be executed.

How to catch GPS off broadcast once?

I have surfed the web and I haven't found a solution to my problem.
In my android app I have to catch and send a notification to the server everytime the user turn off the GPS. At this time I have writed this code
In the Android manifiest:
<receiver android:name="proguide.prosegur.scr.BL.receivers.GPSStatusBroadcastReceiver">
<intent-filter>
<action android:name="android.location.PROVIDERS_CHANGED" />
</intent-filter>
</receiver>
In the GPSStatusBroadcastReceiver class:
public class GPSStatusBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent arg1) {
if (arg1.getAction().matches("android.location.PROVIDERS_CHANGED")) {
// here I have to send the notification
}
}
The problem is that everytime the user put down the GPS, I get this function called twice with identical Context and Intent arguments (I can only send 1 notification at a time).
Important note: it has to work under API level 8.
So, why this happen twice? What can I do (doing it right, not messing up the code) to send only 1 notification at a time? Thanks, sorry for my English.
Try this:
public class GpsReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO) {
final String action = intent.getAction();
if (action.equals(LocationManager.PROVIDERS_CHANGED_ACTION)) {
// GPS is switched off.
if (!context.getSystemService(Context.LOCATION_SERVICE).isProviderEnabled(LocationManager.GPS_PROVIDER)) {
// Do something.
}
}
}
}
}
Also, instead of hardcoding "android.location.PROVIDERS_CHANGED", you should use the variable LocationManager.PROVIDERS_CHANGED_ACTION provided by Android.
Instead of setting your GPS receiver in your AndroidManifest.xml file, register your GPS receiver via a Service as follow:
public class GpsService extends Service {
private BroadcastReceiver mGpsReceiver;
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
registerReceiver();
return Service.START_NOT_STICKY;
}
private void registerReceiver() {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO) {
IntentFilter mIntentFilter = new IntentFilter();
mIntentFilter.addAction(LocationManager.PROVIDERS_CHANGED_ACTION);
this.mGpsReceiver = new GpsReceiver();
this.registerReceiver(this.mGpsReceiver, mIntentFilter);
}
}
}
You can avoid this problem using sharedpreference and with an thread
but it is not a proper way to overcome this problem
my method as follows
#Override
public void onReceive(Context context, Intent intent) {
boolean flage=MainActivity.getpreference();
if(!flage){
MainActivity.putPreferens(true);
Log.e("gpssss","gpssss");
Thread thread = new Thread() {
#Override
public void run() {
try {
sleep(2000);
MainActivity.putPreferens(false);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
}}
}
to the main class am create a sharedpreference and store boolean value false
the broad cast will work once.

Android Service Running as a separate process

I have a service Class in android. Is it be possible for a Service to run as a separate process than an application just for receiving SMS and enqueue them in a queue after that an application reads SMS from this Queue.
Is it possible to launch a separate service?
I have tag the source code of SmsService class below
public class SmsService extends Service {
private SMSReceiver mSMSreceiver;
private IntentFilter mIntentFilter;
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public SmsService(){
/*dba = new DataBaseAdapter(this);*/
mSMSreceiver = new SMSReceiver();
}
#Override
public void onCreate(){
super.onCreate();
mIntentFilter = new IntentFilter();
mIntentFilter.addAction(ConstantClass.SMS_RECEIVED);
registerReceiver(mSMSreceiver,mIntentFilter);
}
#Override
public int onStartCommand(Intent intent , int flags, int type){
return START_STICKY;
}
#Override
public void onDestroy(){
super.onDestroy();
//unregisterReceiver(mSMSreceiver);
}
To enroll your service in a different process, you need to define android:process attribute when defining your service in AndroidManifest.xml
For example:
<service android:process=":kaushik" />
This will run your service in a new process called kaushik.

Register Battery Changed broadcast in Android service

I'm starting a service from an Activity. The service registers for Battery Changed broadcast Receiver. I receive broadcasts as long as the screen is ON. Once the screen is turned OFF, I stop receiving broadcasts, however, the service doesn't die.
My activity code,
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = this.getApplicationContext();
Intent intent = new Intent(this,BatteryStatusService.class);
startService(intent);
}
and my service code,
public class BatteryStatusService extends Service{
private final static String TAG = BatteryStatusService.class.getSimpleName();
private BroadcastReceiver timeTickReceiver;//changeReceiver;
private boolean registered = false;
public class LocalBinder extends Binder {
BatteryStatusService getService() {
return BatteryStatusService.this;
}
}
private final IBinder mBinder = new LocalBinder();
#Override
public void onStart(Intent intent, int startId){
Log.i(TAG,"Starting service");
IntentFilter filter = new IntentFilter();
filter.addAction(Constants.ACTION_BATTERY_CHANGED);
timeTickReceiver = new TimeTickReceiver();
this.getApplicationContext().registerReceiver(timeTickReceiver, filter);
registered = true;
}
#Override
public void onDestroy(){
Log.d(TAG,"Stopping service");
if(registered){
this.getApplicationContext().unregisterReceiver(timeTickReceiver);
}
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return mBinder;
}
public class TimeTickReceiver extends BroadcastReceiver {
private String action = null;
private final String TAG = TimeTickReceiver.class.getSimpleName();
#Override
public void onReceive(Context context, Intent intent) {
action = intent.getAction();
if(action.equals(Constants.ACTION_BATTERY_CHANGED)){
Log.d(TAG,"I got action = "+action);
}
}
}
}
}
use AlarmManager and get last broadcasted level with
Intent BATTERYintent=this.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
However there are mobiles where it would work either. I have t-mobile MOVE which will not update battery value/broadcast no matter what in sleep mode... but most mobiles will do it as they should
by the way dont listen to dcanh121 there are cases when u need to get battery level even when phone is in sleepmode.
Once the screen is turned OFF, I stop receiving broadcasts, however, the service doesn't die.
When the screen is turned off, shortly thereafter the device goes into sleep mode. Your code does not execute again until something wakes up the device from sleep mode.
Also:
You do not need to use getApplicationContext() here
You do not need a Binder here, since you are not binding to the service, so just have onBind() return null
You need to have some code somewhere to stop this service, so it does not run forever
why don't you try by using onResume() and onPause()

Categories

Resources