How to ensure Android program process is not to be ended without the last resort

Source: Internet
Author: User

Recently, do a call system comes with a camera's so a function, encountered the pit, recorded here.
Equipment: Red Rice note4

Cause of the problem

Because of the custom camera, it is difficult to meet all the needs of customers, such as: self-timer support, optimization and so on. These aspects of the custom camera is not better than the system, because some systems are custom-made, there will inevitably be a wonderful problem. For example: You run on this phone, no problem, but after you change a phone, the problem arises.

For example: Millet Red Rice Series, you enable the system comes with the camera function, all the current process will be ended (including: other running programs). When you finish the photo and return, the problem comes up and you always report some empty pointers.

Although the system camera is called, the process is ended, but the process that is ended when it is returned is restarted, but the values you previously saved are gone (the activity restarts the OnCreate process and application is reinitialized).
Of course you will say that you can save the value in the Onsaveinstancestate method in the activity, and then recover and then remove it. But what if it's application? That's how it recovers. If your business is complex enough, the data is really bad recovery, such as: your main body is activity,activity and a number of fragment, each fragment and value in application, these are troublesome, recovery is troublesome.

Solution Solutions

Running the service in the foreground
See Official Description: https://developer.android.com/guide/components/services.html

The front-end service is considered a service that the user is actively aware of, so the system does not consider terminating it when memory is low. The foreground service must provide notifications for the status bar, which is placed under the "in Progress" heading, which means that notifications cannot be cleared unless the service is stopped or removed from the foreground.
For example, a music player that plays music through a service should be set to run in the foreground because the user is clearly aware of its actions. Notifications in the status bar may indicate songs that are playing and allow users to initiate Activity to interact with the music player.

Need to use the Startforeground method in the service

Notification Notification = new Notification (R. drawable. Icon, GetText (R. String. Ticker_text), System. Currenttimemillis());Intent notificationintent = new Intent (This, exampleactivity. Class);Pendingintent pendingintent = pendingintent. Getactivity(This,0, Notificationintent,0);Notification. Setlatesteventinfo(This, GetText (R. String. Notification_title), GetText (R. String. Notification_message), pendingintent);Startforeground (ongoing_notification_id, NOTIFICATION);
Implementing Code Notificationservice.java
 PackagexxxImportAndroid.app.Notification;ImportAndroid.app.NotificationManager;ImportAndroid.app.Service;ImportAndroid.content.Context;ImportAndroid.content.Intent;ImportAndroid.media.RingtoneManager;ImportAndroid.net.Uri;ImportAndroid.os.IBinder;ImportAndroid.support.v4.app.NotificationCompat;ImportAndroid.util.Log;ImportCom.hkcg.mrsi.MyApplication;ImportCOM.HKCG.MRSI.R;/** * * @author Aaron * @version 1.0 * @createTime 2016-12-20 * @desc <br> * @usage * * Public  class notificationservice extends Service {    Private StaticUri Alarmsound = Ringtonemanager.getdefaulturi (ringtonemanager.type_notification);Private StaticNotificationmanager Mnotificationmanager = (Notificationmanager) myapplication.getinstance (). GetSystemService ( Context.notification_service);Private Static FinalString TAG ="Notificationservice";@Override     Public void onCreate() {//TODO auto-generated method stubLOG.I (TAG,"OnCreate");Super. OnCreate (); }@Override     PublicIBinderOnbind(Intent Intent) {//TODO auto-generated method stub        return NULL; }@Override     Public int Onstartcommand(Intent Intent,intFlagsintStartid) {log.i (TAG,"Onstartcommand"); Notification Notification = Createnotificationtip (0x689, This,"title","to ensure proper operation, do not end the program running. ",NULL); Startforeground (0x689, notification); Flags = Start_sticky;return Super. Onstartcommand (Intent, flags, Startid); } PublicNotificationCreatenotificationtip(intId,context Context, string title, string content, Integer icon) {icon = icon = =NULL?        R.drawable.ic_launcher:icon; Notificationcompat.builder Mbuilder =NewNotificationcompat.builder (context). Setsmallicon (Icon)//Small Icons. Setcontenttitle (title)//Title. Setcontenttext (content)//Content. setongoing (true)//Whether it is in progress. setpriority (Notification.priority_max);//Notification PriorityNotification Notification = Mbuilder.build (); Mnotificationmanager.notify (ID, notification);returnNotification }}
Androidmanifest.xml
        <service            android:name="com.hkcg.mrsi.loc.NotificationService"            android:enabled="true" >        </service>
Call
startService(new Intent(this, NotificationService.class));

How to ensure that the Android program process is not ended as a last resort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.