service force close on boot up - android

i am trying to start the service at the start up, but the service getting forced closed
the code for Broadcast receiver is as follows
package com.android.locationservice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyLocationBroadcastReceiver extends BroadcastReceiver{
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent();
startServiceIntent.setAction("com.android.locationservice.LocatonTraceService");
context.startActivity(startServiceIntent);
}
}
and the manifest look like this
<receiver android:name="com.android.locationservice.MyLocationBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".LocationTraceService">
<intent-filter>
<action android:name="com.android.locationservice.LocationTraceService" />
</intent-filter>
</service>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
and service implemented like this
package com.android.locationservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.TextView;
import android.widget.Toast;
public class LocationTraceService extends Service
{
#Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service created...", Toast.LENGTH_LONG).show();
// Log.i(tag, "Service created...");
// new LocationActivity("service created...");
}
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Toast.makeText(this, "Service started...", Toast.LENGTH_LONG).show();
//new LocationActivity("service started...");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
return super.onStartCommand(intent, flags, startId);
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show();
//new LocationActivity("service destroyed...");
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
The logcat is
08-25 07:33:39.021: E/AndroidRuntime(248): FATAL EXCEPTION: main
08-25 07:33:39.021: E/AndroidRuntime(248): java.lang.RuntimeException: Unable to start context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
08-25 07:33:39.021: E/AndroidRuntime(248): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2821)
08-25 07:33:39.021: E/AndroidRuntime(248): at android.app.ActivityThread.access$3200(ActivityThread.java:125)
08-25 07:33:39.021: E/AndroidRuntime(248): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
please help me i am new for android ,thanks in advance.

Why are you start service activity, not service?
Correct:
public class MyLocationBroadcastReceiver extends BroadcastReceiver{
public void onReceive(Context context, Intent intent) {
startService(new Intent(context, LocatonTraceService.class));
}
}

I think you are missing GPS Permission in AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_GPS"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

Related

Unable to print log when boot is completed

I am unabe to load a message(log) when boot is completed. I have tried with below code:
In BootReceiver.java file:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Booting Completed", Toast.LENGTH_LONG).show();
Log.d("On Boot Load", "Boot loading");
}
}
In Services.java file:
public class UpdateService extends Service {
#Nullable
#Override
public IBinder onBind(Intent intent) {
Log.d("Service", "Updating Services");
return null;
}
}
I have also added with below code in menifes.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name=".receivers.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
When I run the app the log does not appear in logcat section.
The receiver tag should be inside application tag and the uses-permission tag should be outside application tag but inside of manifest tag.
You should use onStartCommand() implementation:
public class UpdateService extends Service {
#Nullable
#Override
public IBinder onBind(Intent intent) {
Log.d("Service", "Updating Services");
return null;
}
#Override
public int onStartCommand(Intent pIntent, int flags, int startId) {
Log.d("On Boot Load", "Boot loading");
return super.onStartCommand(pIntent, flags, startId);
}
}
Also beware of other filter like android.intent.action.QUICKBOOT_POWERON.
I use this code (without a service) and it works for me (just tested it):
AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name=".MyBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Kotlin:
class MyBootReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == ACTION) {
Logger.warning("BOOTING UP!")
}
}
companion object {
const val ACTION = "android.intent.action.BOOT_COMPLETED"
}
}

MediaPlayer Service not working in Android Studio

I'm using the following code for MediaPlayer services. But the following service is not running. The toasts are being triggered but the player doesn't play any song.
The 'onStart()' method is deprecated as I override it.
package com.example.user.stringremoval;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;
public class MyService extends Service {
MediaPlayer mediaPlayer;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate()
{
Toast.makeText(this, "Service Created", Toast.LENGTH_SHORT).show();
mediaPlayer=MediaPlayer.create(this,R.raw.bet);
mediaPlayer.setVolume(50,50);
mediaPlayer.setLooping(false);
}
#Override
public void onStart(Intent intent,int startId)
{
Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
mediaPlayer.start();
}
#Override
public void onDestroy()
{
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show();
mediaPlayer.stop();
}
}
Error in Logcat:
06-27 12:47:03.175 3022-3022/com.example.user.stringremoval E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
Activity class:
package com.example.user.stringremoval;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class AudioPlayer extends AppCompatActivity implements View.OnClickListener {
Button bn1,bn2,bn3;
Intent intent1=getIntent();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio_player);
bn1=(Button)findViewById(R.id.button3);
bn2=(Button)findViewById(R.id.button4);
bn3=(Button)findViewById(R.id.button5);
bn1.setOnClickListener(this);
bn2.setOnClickListener(this);
bn3.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.button3:
startService(new Intent(this,MyService.class));
break;
case R.id.button4:
stopService(new Intent(this,MyService.class));
break;
case R.id.button5:
Intent intent=new Intent(this,MainActivity.class);
startActivity(intent);
}
}
}
Android Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.stringremoval">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AudioPlayer"></activity>
<service android:name=".MyService"
android:enabled="true"/>
</application>
</manifest>
Can anyone help me to resolve this problem?
You have to Override onStartCommand() method and return START_STICKY have look this!
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// play mp3 code here
return START_STICKY;
}
refer this link for better understanding of above method and START_STICKY.
Hope it will help you!
Try this code and define below things into your code..
// this line define into activity class into oncrateview method.
startService(new Intent(MyActivity.this, MyService.class));
add below line into android manifest file ..
between application tag..
<service android:enabled="true" android:name="com.my.packagename.MyService" />

Cannot start Service in API 23

I am trying to experiment with services, and I found this example:
Alarm Manager Example.
I tried using the accepted answer, and the changes AndroidDev suggested, but I cant get it to work. Based on the fact that none of my Log messages appear, I think that the Service isn't called.
My onCreate method is this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Intent serviceIntent = new Intent(this, YourService.class);
startService(serviceIntent);
Vibrator v=(Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
v.vibrate(500);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
the Service is this:
package habos.alarmtest;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class YourService extends Service
{
Alarm alarm = new Alarm();
String tag="Babis service";
public void onCreate()
{
super.onCreate();
Log.i(tag, "oncreate");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.i(tag, "onstartcommand");
alarm.setAlarm(this);
return START_STICKY;
}
#Override
public void onStart(Intent intent, int startId)
{
alarm.setAlarm(this);
}
#Override
public IBinder onBind(Intent intent)
{
return null;
}
}
The onStart method is deprecated, but onStartCommand should work.
the manifest is this
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="habos.alarmtest">
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<receiver android:name=".Alarm" android:exported="true">
<intent-filter>
<action android:name="habos.START_ALARM" >
</action>
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
and the alarm is this
package habos.alarmtest;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.util.Log;
import android.widget.Toast;
import android.os.Vibrator;
public class Alarm extends BroadcastReceiver
{
String tag="Babis alarm";
#Override
public void onReceive(Context context, Intent intent)
{
Log.i(tag, "onreceive");
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
// Put here YOUR code.
Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example
Vibrator v=(Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
v.vibrate(500);
wl.release();
}
public void setAlarm(Context context)
{
Log.i(tag,"setalarm");
AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent("mypackage.START_ALARM");
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 30,pi); // Millisec * Second * Minute
}
public void cancelAlarm(Context context)
{
Intent intent = new Intent(context, Alarm.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
}
added 2 more Logs, before and after the startService,the logcat is here:
https://www.dropbox.com/s/0ksdrfzyjb0qafe/logcat.txt?dl=0
(sorry I didn't paste it directly here, the code block didn't work for some reason)

Always running Service with BroadcastReceiver for Call Tapping

My Requirment
I am trying to build an application which shows a Toast whenever user makes a outgoing call from the phone. For this I am using a BroadcastReceiver to tap the call action and a service (to run Receiver always). once I start this activity, it starts showing toast when a outgoing call get initiated ...everything works well.
Issues
Sometimes I dont get any Toast Action EVEN for Outgoing call as well, is that mean service stops after some time ?
After phone reboot Toast Action for Outgoing call stops. Untill you start the servce again manually.
The code I have written is ok ? OR can it be improved ?
Below is the complete code -
MainActivity.class
public class MainActivity extends Activity
{
CallNotifierService m_service;
boolean isBound = false;
private ServiceConnection m_serviceConnection = new ServiceConnection()
{
#Override
public void onServiceConnected(ComponentName className, IBinder service)
{
m_service = ((CallNotifierService.MyBinder)service).getService();
Toast.makeText(MainActivity.this, "Service Connected", Toast.LENGTH_LONG).show();
isBound = true;
Intent intent = new Intent(MainActivity.this, CallNotifierService.class);
startService(intent);
}
#Override
public void onServiceDisconnected(ComponentName className)
{
m_service = null;
isBound = false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, CallNotifierService.class);
bindService(intent, m_serviceConnection, Context.BIND_AUTO_CREATE);
}
.
.
.
}
CallNotifierService.class
public class CallNotifierService extends Service
{
private final IBinder myBinder = new MyBinder();
private static final String ACTION_OUTGOING_CALL = "android.intent.action.NEW_OUTGOING_CALL";
private static final String ACTION_ANSWER = "android.intent.action.ANSWER";
private static final String ACTION_CALL = "android.intent.action.CALL";
private CallBr br_call;
#Override
public IBinder onBind(Intent arg0)
{
return myBinder;
}
#Override
public void onDestroy()
{
Log.d("service", "destroy");
this.unregisterReceiver(this.br_call);
Toast.makeText(CallNotifierService.this, "Receiver Un-Registered", Toast.LENGTH_LONG).show();
super.onDestroy();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
final IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_ANSWER);
filter.addAction(ACTION_CALL);
filter.addAction(ACTION_OUTGOING_CALL);
this.br_call = new CallBr();
this.registerReceiver(this.br_call, filter);
Toast.makeText(CallNotifierService.this, "onStartCommand Called", Toast.LENGTH_LONG).show();
return START_STICKY;
}
public class MyBinder extends Binder
{
CallNotifierService getService()
{
return CallNotifierService.this;
}
}
public class CallBr extends BroadcastReceiver
{
public CallBr() {}
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Action:"+intent.getAction(), Toast.LENGTH_LONG).show();
}
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alwaysrunningprocesswithcallanswertap"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-sdk
android:minSdkVersion="22"
android:targetSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.example.alwaysrunningprocesswithcallanswertap.CallNotifierService" />
</application>
</manifest>
Could anyone please help for the issue OR pointout something better ?
#Bharat Once device switch off automatically service will stop, So when ever your device reboot that time you need to start service again then only you will get Toast Action, refer this example code :
BootCompleteReceiver.java
package com.example.newbootservice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootCompleteReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, AutostartService.class);
context.startService(service);
}
}
AutostartService .java
package com.example.newbootservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class AutostartService extends Service {
#Override
public void onCreate() {
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
return Service.START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroy", Toast.LENGTH_LONG).show();
}
#Override
public IBinder onBind(Intent arg0) {
return null;
}
}
MainActivity.java
package com.example.newbootservice;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startService(new Intent(getBaseContext(), AutostartService.class));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.newbootservice"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<service android:name=".AutostartService"/>
<receiver android:name=".BootCompleteReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Service methods not working in android

I just created a service as shown below :
package com.example.timepass;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Toast;
public class alarm extends Service{
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Toast.makeText(this, "Entered in service", Toast.LENGTH_SHORT).show();
}
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "onStartCommand...", Toast.LENGTH_LONG).show();
return 1;
// Log.i("YourService", "Yes this works.");
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show();
}
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
Toast.makeText(this, "Changed", Toast.LENGTH_SHORT).show();
return null;
}
}
Now when I startservice from mainactivity by the following command:
Intent myIntent = new Intent("com.example.timepass.ALARM");
MainActivity.this.startService(myIntent);
By doing this there is no error, but no TOAST of Service class are dipslayed
My manifest is :
<service class=".alarm" android:name=".alarm" android:enabled="true">
<intent-filter>
<action android:value="com.example.timepass.ALARM"
android:name=".alarm" />
</intent-filter>
</service>
Please guide me!!!
Probably you don't have the service in your manifest, or it does not have an that matches your action. Examining LogCat should turn up some warnings that may help.
More likely, you should start the service via:
startService(new Intent(this, alarm.class));

Categories

Resources