2017-06-20 97 views
2

無法使用InstantApp功能模塊內的前臺服務。獲取運行時安全性異常。Android InstantApp:前臺服務

java.lang.RuntimeException:無法啓動活動 ComponentInfo { .XYZActivity}:java.lang.SecurityException異常:不可 即時應用

Android的文件說的方法 類android.app.ActivityManagerProxy.getServices,

限制功能:上運行該設備沒有用戶意識到。 提供前臺服務。即時應用程序只能通過支持應用程序鏈接的活動啓動,因此服務, 內容提供商或廣播接收器將無法啓動您的 應用程序。

代碼:

// Starting service 
getAppContext().startService(new Intent(getAppContext(), FirebaseAuthService.class)); 


// Foreground service class 
public class FirebaseAuthService extends Service { 

    private static final String TAG = "FirebaseAuthService"; 
    private boolean isRunning = false; 

    private String mUserId; 
    private FirebaseAuth mAuth; 

    @Override 
    public void onCreate() { 
     Log.d(TAG, "Service onCreate"); 

     startForeground(); 
     isRunning = true; 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Log.i(TAG, "Service onStartCommand"); 

     new Thread(new Runnable() { 
      @Override 
      public void run() { 
       myTask(); 
      } 
     }).start(); 

     return Service.START_STICKY; 
    } 


    @Override 
    public IBinder onBind(Intent arg0) { 
     Log.i(TAG, "Service onBind"); 
     return null; 
    } 

    @Override 
    public void onDestroy() { 
     isRunning = false; 
     Log.i(TAG, "Service onDestroy"); 
    } 

    private void startForeground() { 
     Intent notificationIntent = new Intent(this, HomeActivity.class); 

     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 

     Notification notification = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.noti_logo) 
       .setContentTitle("Title") 
       .setContentText("Preparing...") 
       .setContentIntent(pendingIntent).build(); 

     startForeground(1337, notification); 
    } 

    private void myTask() { 
     // At end 
     // Stop service once it finishes its task 
     stopSelf(); 
    } 
} 
+0

快速問題:您能否確認從PendingIntent.getActivity返回的掛起的意圖不爲null?並不是說它與您遇到的問題直接相關,而是我看到沒有未定義的意圖被創建。 –

回答

2

你的代碼是正確的,但前臺服務不是目前由於即時應用主管一個已知問題的工作。

+1

你有機票ID嗎? – mol

+0

不幸的是不公開可見。 – ibrahimkarahan

+0

已解決問題:https://github.com/googlesamples/android-instant-apps/issues/15 –