Android: simple start and stop for service via JNIActivity
This commit is contained in:
parent
5e9ed25c01
commit
77b8b8fd8a
|
@ -16,6 +16,7 @@
|
|||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.BLUETOOTH" />
|
||||
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_SERVICE" />
|
||||
|
||||
<uses-feature
|
||||
android:glEsVersion="0x00020000"
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package com.reicast.emulator;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.ActivityManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.res.Configuration;
|
||||
import android.net.Uri;
|
||||
|
@ -19,6 +22,7 @@ import android.view.WindowManager;
|
|||
import android.widget.PopupWindow;
|
||||
|
||||
import com.reicast.emulator.config.Config;
|
||||
import com.reicast.emulator.emu.EmuService;
|
||||
import com.reicast.emulator.emu.GL2JNIView;
|
||||
import com.reicast.emulator.emu.JNIdc;
|
||||
import com.reicast.emulator.emu.OnScreenMenu;
|
||||
|
@ -35,6 +39,7 @@ import java.util.HashMap;
|
|||
import tv.ouya.console.api.OuyaController;
|
||||
|
||||
public class GL2JNIActivity extends Activity {
|
||||
private Intent serviceIntent;
|
||||
public GL2JNIView mView;
|
||||
OnScreenMenu menu;
|
||||
public MainPopup popUp;
|
||||
|
@ -56,6 +61,11 @@ public class GL2JNIActivity extends Activity {
|
|||
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
|
||||
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
|
||||
}
|
||||
serviceIntent = new Intent(this, EmuService.class);
|
||||
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_FROM_BACKGROUND);
|
||||
serviceIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES | Intent.FLAG_ACTIVITY_NO_ANIMATION);
|
||||
startService(serviceIntent);
|
||||
|
||||
Emulator app = (Emulator)getApplicationContext();
|
||||
app.getConfigurationPrefs(prefs);
|
||||
menu = new OnScreenMenu(GL2JNIActivity.this, prefs);
|
||||
|
@ -536,6 +546,22 @@ public class GL2JNIActivity extends Activity {
|
|||
return true;
|
||||
}
|
||||
|
||||
public boolean serviceRunning(Class<?> javaclass) {
|
||||
ActivityManager manager = (ActivityManager)
|
||||
getSystemService(Context.ACTIVITY_SERVICE);
|
||||
try {
|
||||
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
|
||||
if (javaclass.getName().equals(
|
||||
service.service.getClassName())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} catch (NullPointerException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
|
@ -547,12 +573,14 @@ public class GL2JNIActivity extends Activity {
|
|||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
moga.onDestroy();
|
||||
if (serviceRunning(EmuService.class))
|
||||
stopService(serviceIntent);
|
||||
JNIdc.stop();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
// TODO Auto-generated method stub
|
||||
JNIdc.stop();
|
||||
mView.onStop();
|
||||
super.onStop();
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import android.app.Service;
|
|||
import android.content.Intent;
|
||||
import android.os.IBinder;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
|
||||
import com.reicast.emulator.R;
|
||||
|
||||
public class EmuService extends Service {
|
||||
|
@ -30,9 +31,9 @@ public class EmuService extends Service {
|
|||
|
||||
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
|
||||
.setSmallIcon(R.drawable.ic_launcher)
|
||||
.setTicker("Emulator ticker message")
|
||||
.setContentTitle("Emulator notification title")
|
||||
.setContentText("Emulator notification content")
|
||||
.setTicker(getString(R.string.service_ticker))
|
||||
.setContentTitle(getString(R.string.service_title))
|
||||
.setContentText(getString(R.string.service_content))
|
||||
.setContentIntent(pendingIntent);
|
||||
startForeground(FOREGROUND_ID, builder.build());
|
||||
return START_STICKY_COMPATIBILITY;
|
||||
|
|
|
@ -129,6 +129,10 @@
|
|||
<string name="downloadVMU">Download VMU</string>
|
||||
|
||||
<string name="log_saved">Logcat saved and copied to clipboard\nPlease paste inside the Github issue</string>
|
||||
|
||||
<string name="service_ticker">reicast is running...</string>
|
||||
<string name="service_title">reicast is running...</string>
|
||||
<string name="service_content">Emulator service is currently running</string>
|
||||
|
||||
<!-- Onscreen Menu -->
|
||||
<string name="popup_button_back">Back</string>
|
||||
|
|
Loading…
Reference in New Issue