2017-06-20 55 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 –