Reputation: 392
I have MainActivity and a custom service name BatteryService.In this Service I am taking battery level and making some work on them. here is a sample code of this service class.
public class BatteryService extends Service {
Context context ;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
context = this;
Log.v("Service Started : ", "" );
BatteryService.this.registerReceiver(brodcastReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
protected BroadcastReceiver brodcastReceiver = new BroadcastReceiver() {
int intlevel;
MediaPlayer mediaPlayer;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
intlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
String dtr;
dtr = String.valueOf(intlevel);
Toast.makeText(context, dtr, Toast.LENGTH_LONG).show();
try {
intlevel = intent.getIntExtra("level", 0);
Log.v("Battery level is : ", "" + dtr);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
String strStatus;
if ((status == BatteryManager.BATTERY_STATUS_CHARGING) && (intlevel >= 90)) {
Toast.makeText(context, "charging", Toast.LENGTH_LONG).show();
mediaPlayer = MediaPlayer.create(BatteryService.this, R.raw.alarmsound);
mediaPlayer.start();
}
if ((status == BatteryManager.BATTERY_STATUS_DISCHARGING) && (intlevel >= 99))
{
mediaPlayer.stop();
}
}
};
}
And in Main Activity I have button and on that button click I am starting service such as :
public class MainActivity extends Activity {
Button btnStart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStart = (Button) findViewById(R.id.btn_startService);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v("Starting service","On Button click");
Intent intent = new Intent(MainActivity.this, BatteryService.class);
startService(intent);
}
});
}
}
and declared it in Manifest in following way
android:name="com.brokenfloorstudios.eyebattery.BatteryService" />
Upvotes: 2
Views: 58
Reputation: 2032
Try this
Intent a = new Intent();
a.setAction (" *** insert service name here**");
StartService(a);
I normally copy and paste the string from the manifest file to my activity as it is so easy to make a spelling mistakes
Upvotes: 0
Reputation: 392
I am posting this answer so that if some one meet the same problem as I faced. It was a little one. As I was making my own custom service , I was declaring it just beneath the closing tag of Application in Manifest file. where as it should be declared in the application tag in manifest.It took time to figure it out as I was not getting any error or exception in the Logcat.
Upvotes: 1