2017-02-28 50 views
2

清單:如何綁定服務,如果它在另一個進程?

<service android:name="com.example.MainService" android:process=":main_service"/> 

試圖在活動綁定服務:

public class MainActivity extends Activity { 
    MainService mMainService; 

    private boolean mBound; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     bindService(intentForMainService, mConnection, Context.BIND_AUTO_CREATE) 
    } 

    private ServiceConnection mConnection = new ServiceConnection() { 
     public void onServiceConnected(ComponentName className, 
             IBinder service) { 
      MainService.MainServiceBinder binder = (MainService.MainServiceBinder) service;//HERE IS EXCEPTION 
      mMainService = (MainService) binder.getService(); 
      mBound = true; 
     } 

     public void onServiceDisconnected(ComponentName className) { 
      mMainService = null; 
      mBound = false; 
     } 
    }; 

    @Override 
    protected void onStop() { 
     doUnbindService(); 
     super.onStop(); 
    } 

    void doUnbindService() { 
     if (mBound) { 
      unbindService(mConnection); 
     } 
    } 
} 

錯誤:

FATAL EXCEPTION: main 
Process: com.hos.android, PID: 9001 
    java.lang.ClassCastException: android.os.BinderProxy cannot be cast to com.example.service.main.MainService$MainServiceBinder 
    at com.example.ui.base.BaseServiceActivity$1.onServiceConnected(MainActivity.java:34) 
    at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1335) 
    at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1352) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:158) 
    at android.app.ActivityThread.main(ActivityThread.java:7224) 

但是,當我刪除此安卓過程= 「:main_service」所有正常工作

回答

4

步驟#1:編寫an AIDL file,描述客戶端可以綁定到的服務要導出的接口。出於這個答案的目的,我將稱這個接口爲Foo,所以AIDL文件將是Foo.aidl。請注意,如果客戶端和服務位於不同的Android Studio模塊中,這兩個模塊都需要相同的內容。

步驟2:讓您的服務活頁夾延伸Foo.Stub並覆蓋Foo.Stub上的方法,而不是延長IBinder

第3步:在您的客戶端,在onServiceConnected(),原始粘結劑通過Foo.Stub.asInterface(service)轉換爲Foo實例,Foo有AIDL定義的API的客戶端。

This pair of sample projects說明了這一點,在我的情況下,客戶端和服務在不同的應用程序。

+0

無法解析方法'myPid()' – NickUnuchek

+0

@NickUnuchek:對不起,但我不知道你的意思。 – CommonsWare

+0

https://developer.android.com/guide/components/aidl.html'Process.myPid()' – NickUnuchek

0

我是這麼認爲的,如果你做這將是很好

所有的弗里斯特創建Servic

public class MyService extends Service { 

MyReceiver receiver = new MyReceiver(); 
@Override 
public void onCreate() { 
    super.onCreate(); 
} 
@Nullable 
@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    receiver.startAlarm(this); 
    return START_STICKY; 
} 

@Override 
public void onStart(Intent intent, int startId) { 
    receiver.startAlarm(this); 
} 

} 

後啓動報警聽報警創建BroadcastReceiver

public class MyReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(final Context context, Intent intent) { 
     Log.e("Receiver", "end alarm") 
    } 
} 


public void setAlarm(Context context) { 
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
    Intent i = new Intent(context, BootReceiver.class); 
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0); 
    am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 10, pi); // Millisec * Second * Minute 
} 

public void cancelAlarm(Context context) { 
    Intent intent = new Intent(context, BootReceiver.class); 
    PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0); 
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
    alarmManager.cancel(sender); 
} 

添加清單此

<service android:name=".MyService" 
     android:enabled="true" 
     android:process=":my_service"/> 

    <receiver 
     android:name="MyReceiver" 
     android:exported="false" > 
     <intent-filter> 
      <action android:name="com.parse.push.intent.RECEIVE" /> 
      <action android:name="com.parse.push.intent.DELETE" /> 
      <action android:name="com.parse.push.intent.OPEN" /> 
     </intent-filter> 
    </receiver> 
相關問題