service with android:process doesn't start - android

I have simple app for testing android:process attribute of service. So my service looks like this:
public class MyService extends IntentService {
static final String TAG = "MyService";
public MyService() {
super("MyService");
}
#Override
public void onCreate() {
super.onCreate();
Log.d(TAG,"onCreate()");
}
#Override
protected void onHandleIntent(Intent intent) {
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
doJob();
return START_STICKY;
}
private void doJob() {
new Thread(new Runnable() {
#Override
public void run() {
Log.i(TAG,"job started");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.i(TAG,"job stopped");
stopSelf();
}
}).start();
}
}
I'm trying to start service from the activity as follows:
findViewById(R.id.startServiceBtn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startService(new Intent(getApplicationContext(),MyService.class));
}
});
All works fine. Now i'm adding android:process=":service" attribute to service declaration in the manifest. And after that service doesnt't start. So why so and what should i change for start service in separate process?

I think you have to do this task i.e dowork() method call in onHandleIntent(). Because your service class extending Intent service instead of Service.
public class MyService extends IntentService {
static final String TAG = "MyService";
public MyService() {
super("MyService");
}
#Override
protected void onHandleIntent(Intent intent) {
doJob();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
}
private void doJob() {
new Thread(new Runnable() {
#Override
public void run() {
Log.i(TAG,"job started");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.i(TAG,"job stopped");
stopSelf();
}
}).start();
}
}

oh sorry it was silly mistake - i have to select new process in android monitor tab) thanks to all

Related

Created Service with accessibility service but unable to stop it

I have created a service. When I start it from MainActivity its started but when I click on button to stop it it's doesn't stop. It is working fine in lower version devices but only issue found in Android 10 or higher versions.
public void startServices() {
Intent intent = new Intent(getApplicationContext(), PerformTaskService.class);
startService(intent);
}
public void stopServices() {
stopService(new Intent(getApplicationContext(), PerformTaskService.class));
}
PerformTaskService.java
public class PerformTaskService extends Service {
String TAG = "ServiceStatus";
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
int id;
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
this.id = startId;
Log.e(TAG, "onStartCommand: "+startId );
return START_NOT_STICKY;
}
#Override
public void onDestroy() {
stopSelfResult(id);
stopSelf(id);
new StatusBarService().onDestroy();
stopSelf();
super.onDestroy();
}
}
StatusBarService.java
public class StatusBarService extends AccessibilityService {
int id;
#Override
public ComponentName startForegroundService(Intent service) {
return super.startForegroundService(service);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
this.id = startId;
return START_NOT_STICKY;
}
#SuppressLint({"InflateParams", "InlinedApi"})
#Override
public void onCreate() {
super.onCreate();
View mFloatingView =
LayoutInflater.from(this).inflate(R.layout.layout_floating_widget,
null);
thumbView =
LayoutInflater.from(this).inflate(R.layout.layout_seekbar_thumb,
null, false);
deviceManger = ((DevicePolicyManager)
getSystemService(Context.DEVICE_POLICY_SERVICE));
}
#Override
public void onAccessibilityEvent(AccessibilityEvent event) {
}
#Override
public void onInterrupt() {
}
public void deviceadminCheck() {
Intent intent = new
Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
#Override
public void onDestroy() {
stopSelf();
stopSelf(id);
stopSelfResult(id);
super.onDestroy();
}
}
So, kindly let me know if you have any proper solution or Idea about this issue. how to stop service

onCreate doesn't run in a service

I'm writing this service but I can't see the log strings when I launch the app.
if I put a Log.i in the method startBeaconDetecting I can see it but after the onCreate() method doesn't start.
Why onCreate doesn't run?
public class ProximityBeacon extends Service
{
private String server;
private String ID;
public ProximityBeacon(String server)
{
this.server=server;
//TODO indirizzo del server
}
public void startBeaconDetecting(String ID, Intent intent, Context context)
{
this.ID=ID;
context.startService(intent);
}
public void stopBeaconDetecting()
{
stopSelf();
onDestroy();
}
public void onCreate()
{
super.onCreate();
Log.i(TAG, "Oncreate");
}
public int onStartCommand(Intent intent, int flag, int startId)
{
Log.i(TAG, "onStartCommand");
return Service.START_STICKY;
}
}
Change your Service class to this:
public class ProximityBeacon extends Service
{
private static final String TAG = "TAG";
private String server;
private String ID;
public ProximityBeacon() {
}
public ProximityBeacon(String server)
{
this.server=server;
//TODO indirizzo del server
}
public void startBeaconDetecting(String ID, Intent intent, Context context)
{
this.ID=ID;
context.startService(intent);
}
public void stopBeaconDetecting()
{
stopSelf();
onDestroy();
}
public void onCreate()
{
super.onCreate();
Log.i(TAG, "ProximityBeacon Oncreate");
}
public int onStartCommand(Intent intent, int flag, int startId)
{
Log.i(TAG, "ProximityBeaconon StartCommand");
return Service.START_STICKY;
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
and then call it using this:
new ProximityBeacon("").startBeaconDetecting("" ,new Intent(HomeActivity.this, ProximityBeacon.class) ,mContext) ;
then add it in manifest.xml like this :
<service android:name=".services.ProximityBeacon" />
Output : 19:25:11.873 23002-23002/com.sample.service I/TAG: ProximityBeaconon StartCommand
after startService(service); will run onStartCommand, not onCreate

How to know if phone app is closed

please explain me how does android call lock screen after closing emergency dialer. I've tried to check every second if emergency call is the top activity if it's not then i just lock device but this didn't do it's job in android 5.0 and higher. So, I need another solution.
My code:
private static final long INTERVAL = TimeUnit.SECONDS.toMillis(1/100);
private Thread t = null;
private Context ctx = null;
private boolean running = false;
public MyService2() {
}
#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 ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
running = true;
ctx = this;
Log.i("Point","PreRun");
t = new Thread(new Runnable() {
#Override
public void run() {
Log.i("Point","preloop");
do {
Log.i("Point", "Prechech");
if (activityManager.getRunningTasks(1).get(0).topActivity.getPackageName().equals("com.android.phone") || activityManager.getRunningTasks(1).get(0).topActivity.getPackageName().equals("com.android.dialer"))
{
try {
Thread.sleep(INTERVAL);
} catch (InterruptedException e) {
Log.i("Point", "Thread interrupted: 'KioskService'");
}
} else {
running=false;
Log.i("Task is",activityManager.getRunningTasks(1).get(0).topActivity.getPackageName());
stopSelf();
}
}while (running) ;
}
});
t.start();
Log.i("Point","Done");
return super.onStartCommand(intent, flags, startId);
}
#Override
public void onDestroy() {
super.onDestroy();
Log.i("Point","Service is stopped");
}
}
And this:
public class MyActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("com.android.phone.EmergencyDialer.DIAL");
startActivity(intent);
Intent intent1 = new Intent(MyActivity.this,MyService2.class);
startService(intent1);
}
});
}
}
You can launch your emergency dialer like this:
Intent intent = new Intent("android.intent.action.MAIN");
intent.setComponent(ComponentName.unflattenFromString("com.android.phone/.EmergencyDialer"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Moreover please check if you have declared your service properly in manifest.
<service
android:name=".MyService2">
</service>

How to call another activity after destroy() of service is called

In this code I have created a service.After 3 seconds i want to destory this service and want to call another activity in destroy method.but this doesnt seem to work.
public class TimerService 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)
{
new CountDownTimer(3000, 1000)
{
public void onTick(long millisUntilFinished)
{
}
public void onFinish()
{
stopSelf();
}
}.start();
return super.onStartCommand(intent,flags,startId);
}
#Override
public void onDestroy()
{
super.onDestroy();
Intent i = new Intent(this,Dummy.class);
i.putExtra("SessionTimedOut","Expire");
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(i);
}
}
Activity which calls My Service
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i = new Intent(this,TimerService.class);
startService(i);
}
}
Try to insert the code to the finish method:
#Override
public int onStartCommand(Intent intent,int flags,int startId)
{
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(this,Dummy.class);
i.putExtra("SessionTimedOut","Expire");
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
stopSelf();
}
}, 3000);
return super.onStartCommand(intent,flags,startId);
}

Service Broadcast Receiver Logs more than expected

My problem is that I think my service runs multiple times. The service is controlled by a switch calling a method that starts and stops the service. I have a broadcast listener inside my service and the receiver logs whenever it receives a broadcast. at first switch on, it logs two times. After switch off and on, it logs four times.
public class Service1 extends Service implements DetectionListener {
private static final String TAG = "ListenerService";
private final broadcastReceiver1 receiver = new broadcastReceiver1();
public HashMap<String, String> hashMapData;
private ARSonicManager arSonicManager;
public ListenerService() {
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
IntentFilter filter = new IntentFilter();
filter.addAction("com.digify.almostrealsonic.broadcast_intent");
registerReceiver(receiver, filter);
}
#Override
public void onDestroy() {
unregisterReceiver(receiver);
super.onDestroy();
}
#Override
public void onStart(Intent intent, int startId) {
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (null != intent) {
try {
hashMapData = (HashMap<String, String>) intent.getSerializableExtra("hashMapData");
// Do something with hashMapData
} catch (Exception e) {
Log.i("onStartCommand", e.toString());
}
}
return super.onStartCommand(intent, flags, startId);
}
#Override
public void onMarkerDetected(String markerKey) {
Intent intent = new Intent();
intent.putExtra("hashMapData", hashMapData);
intent.setAction("com.broadcastReceiver1_intent");
sendBroadcast(intent);
}
public static class broadcastReceiver1 extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String hashMapData = intent.getStringExtra("hashMapData");
Log.i("receiver", "Got message: " + hashMapData);
}
}
}

Categories

Resources