I tested with the PendingIntent approach and couldn't find anything wrong. That seems to be working fine. However, for reference, here is what I tried at my end:
public class MyService extends Service {
public static final String SERVICE_COMMAND = "COMMAND";
public static final String TAG = "MyService";
public static final int SERVICE_START = 0x01;
public static final int START_ACTIVITY = 0x02;
private class ServiceHandler extends Handler {
public ServiceHandler(HandlerThread thread) {
super(thread.getLooper());
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case START_ACTIVITY:
Intent ActivityIntent = new Intent(getApplicationContext(), TestActivity.class);
ActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),
0, ActivityIntent, 0);
try {
pendingIntent.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
Log.e(TAG, "Service started activity -> " + System.currentTimeMillis());
default:
break;
}
super.handleMessage(msg);
}
}
private HandlerThread mThread;
private ServiceHandler mHandler;
public MyService() {
mThread = new HandlerThread("ServiceThread");
mThread.start();
mHandler = new ServiceHandler(mThread);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int id = intent.getIntExtra(SERVICE_COMMAND, SERVICE_START);
switch (id) {
case START_ACTIVITY:
Log.e(TAG, "onStartCommand Service -> " + System.currentTimeMillis());
mHandler.sendEmptyMessage(START_ACTIVITY);
break;
default:
break;
}
return START_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
Activity Code:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "Main";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e(TAG, "onCreate main activity -> " + System.currentTimeMillis());
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
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();
}
});
Intent intent = new Intent(this, MyService.class);
startService(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPause() {
super.onPause();
Log.e(TAG, "onPause main activity -> " + System.currentTimeMillis());
Intent intent = new Intent(this, MyService.class);
intent.putExtra(MyService.SERVICE_COMMAND, MyService.START_ACTIVITY);
startService(intent);
}
}
You can try the same code at your end. May be it can help.