如何保證android程式進程不到萬不得已的情況下,不會被結束

來源:互聯網
上載者:User

標籤:word   null   processor   version   java   http   led   null 指標   split   

最近,做一個調用系統內建相機的那麼一個功能,遇到的坑,在此記錄一下。
裝置:紅米note4

問題起因

因為自訂的相機,很難滿足客戶的所有需要,比如:自拍杆的支援,最佳化方面等等。這些方面自訂的相機都不比系統內建的好,因為有些系統都是商家定製的,難免會出現一個奇葩的問題。比如:你在這款手機上運行,無任何問題,然而你換一款手機後,問題就出現了。

比如:小米的紅米系列,你啟用系統內建拍照功能後,當前的所有進程都會被結束了(包含:其它正在啟動並執行程式)。當你拍照完成,返回時,問題出現了,總會報些null 指標。

雖然調用了系統相機,進程會被結束掉,但返回時被結束的進程被重新啟動,但這時你之前儲存的值都沒了(activity會重新啟動oncreate流程,application會重新初始化)。
當然你會說,完全可以在activity中的onSaveInstanceState方法中儲存值,等恢複再取出。但如果是application呢?那又怎麼恢複。如果你的業務夠複雜,那資料真不好恢複,比如:你的主體是activity,activity中又有若干個Fragment,各個Fragment又取值於application,這些就麻煩多多,恢複都麻煩。

解決方案

在前台運行服務
看官方描述:https://developer.android.com/guide/components/services.html

前台服務被認為是使用者主動意識到的一種服務,因此在記憶體不足時,系統也不會考慮將其終止。 前台服務必須為狀態列提供通知,放在“進行中”標題下方,這意味著除非服務停止或從前台移除,否則不能清除通知。
例如,應該將通過服務播放音樂的音樂播放器設定為在前台運行,這是因為使用者明確意識到其操作。 狀態列中的通知可能表示現正播放的歌曲,並允許使用者啟動 Activity 來與音樂播放器進行互動。

需要在服務裡使用startForeground方法

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);
實現代碼NotificationService.java
package xxx;import android.app.Notification;import android.app.NotificationManager;import android.app.Service;import android.content.Context;import android.content.Intent;import android.media.RingtoneManager;import android.net.Uri;import android.os.IBinder;import android.support.v4.app.NotificationCompat;import android.util.Log;import com.hkcg.mrsi.MyApplication;import com.hkcg.mrsi.R;/** *  * @author Aaron * @version 1.0 * @createTime 2016-12-20 * @desc <br> * @usage */public class NotificationService extends Service {    private static Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);     private static NotificationManager mNotificationManager = (NotificationManager) MyApplication.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);    private static final String TAG = "NotificationService";    @Override    public void onCreate() {        // TODO Auto-generated method stub        Log.i(TAG, "onCreate");        super.onCreate();    }    @Override    public IBinder onBind(Intent intent) {        // TODO Auto-generated method stub        return null;    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        Log.i(TAG, "onStartCommand");        Notification notification = createNotificationTip(0x689,this, "title", "為確保正常運行,請不要結束程式運行。",null);        startForeground(0x689, notification);        flags = START_STICKY;        return super.onStartCommand(intent, flags, startId);    }    public Notification createNotificationTip(int id,Context context, String title, String content, Integer icon) {        icon = icon == null ? R.drawable.ic_launcher : icon;        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)                .setSmallIcon(icon)//小表徵圖                .setContentTitle(title) // 標題                .setContentText(content)// 內容                .setOngoing(true) // 是否進行中                .setPriority(Notification.PRIORITY_MAX);//通知優先順序        Notification notification = mBuilder.build();        mNotificationManager.notify(id, notification);        return notification;    }}
AndroidManifest.xml
        <service            android:name="com.hkcg.mrsi.loc.NotificationService"            android:enabled="true" >        </service>
調用
startService(new Intent(this, NotificationService.class));

如何保證android程式進程不到萬不得已的情況下,不會被結束

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.