The main tasks of the program include the execution of methods in the background, which uses Android Services.
It is necessary to start the Service in such a way that after the MainActivity is closed, the service remains in working condition and can generate notifications, and also, in the event of a system shutdown or a reboot, it is restored on its own.
At the moment, the main problem is to maintain life in MyService after closing the user interface.
AutoStart:
In AndroidManifest.xml were written
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETE"/> <receiver android:name="ru.package.BootReceiver"> <intent-filter> <action android:name = "android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver> In BootReceiver.java:
@Override public void onReceive(Context context, Intent intent) { Intent serviceIntent = new Intent(context, MyService.class); context.startService(serviceIntent); } Service:
In AndroidManifest.xml spelled:
<service android:name="ru.package.MyService" android:enabled="true" android:exported="true" android:process=":AnyProcessName"> </service> MyService.java contains the following code, with the exception of some parts (the most important elements are described, which may contain errors that do not allow solving the set task):
public void onCreate() { super.onCreate(); notificationManager = (NotificationManager) this.getSystemService(this.NOTIFICATION_SERVICE); } public int onStartCommand(Intent intent, int flags, int startId) { sendNotification("Service started", "Service", "started"); return Service.START_STICKY; } public void sendNotification(String Ticker,String Title,String Text) { Intent notificationIntent = new Intent(this, MainActivity.class); notificationIntent.setAction(Intent.ACTION_MAIN); notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER); PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setContentIntent(contentIntent) .setOngoing(true) //invulnerable .setTicker(Ticker) .setContentTitle(Title) .setContentText(Text) .setWhen(System.currentTimeMillis()); Notification notification; if (android.os.Build.VERSION.SDK_INT<=15) { notification = builder.getNotification(); // API 15 and lower }else { notification = builder.build(); } startForeground(DEFAULT_NOTIFICATION_ID,notification); @Override public IBinder onBind(Intent intent) { return new Binder(); } @Override public void onRebind(Intent intent) { super.onRebind(intent); } @Override public boolean onUnbind(Intent intent) { return super.onUnbind(intent); } @Override public void onDestroy() { //Removing any notifications notificationManager.cancel(DEFAULT_NOTIFICATION_ID); //Disabling service stopSelf(); super.onDestroy(); } MyService from MainActivity starts as follows:
Intent intentService; @Override protected void onCreate(Bundle savedInstanceState) { intentService = new Intent(this,MyService.class); } public void runService() { boolean working = isMyServiceRunning(MyService.class); if (!working) { startService(intentService); } else { stopService(intentService); } } public boolean isMyServiceRunning(Class<?> serviceClass) { ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { if (serviceClass.getName().equals(service.service.getClassName())) { return true; } } return false; } Please indicate the mistakes made during programming, adding them with third-party materials (examples) or keywords to search for.
ServiceorIntentService? - Yuriy SPb ♦public class MyService extends Service{. It is necessary to replace with:public class MyService extends IntentService {Adding:public MyService() { super("anyname"); }@Override protected void onHandleIntent(Intent intent) { }public MyService() { super("anyname"); }@Override protected void onHandleIntent(Intent intent) { }? - Mikhail Kolesnikov