2013-05-02 30 views
5

我有一個包含服務的Android應用程序。現在我想在另一個應用程序中訪問該服務。我怎樣才能做到這一點?我發現這個應用程序通過網絡。請找代碼片段波紋管在另一個應用程序中使用應用程序服務

1> 
public class LocalWordService extends Service { 

    private final IBinder mBinder = new MyBinder(); 
    private ArrayList<String> list = new ArrayList<String>(); 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 

     Random random = new Random(); 
     if (random.nextBoolean()) { 
      list.add("Linux"); 
     } 
     if (random.nextBoolean()) { 
      list.add("Android"); 
     } 
     if (random.nextBoolean()) { 
      list.add("iPhone"); 
     } 
     if (random.nextBoolean()) { 
      list.add("Windows7"); 
     } 
     if (list.size() >= 20) { 
      list.remove(0); 
     } 
     return Service.START_NOT_STICKY; 
    } 

    @Override 
    public IBinder onBind(Intent arg0) { 
     return mBinder; 
    } 

    public class MyBinder extends Binder { 
     LocalWordService getService() { 
      return LocalWordService.this; 
     } 
    } 

    public List<String> getWordList() { 
     return list; 
    } 

} 


2> 

public class MyScheduleReceiver extends BroadcastReceiver { 


    // Restart service every 30 seconds 
     private static final long REPEAT_TIME = 1000 * 30; 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      AlarmManager service = (AlarmManager) context 
        .getSystemService(Context.ALARM_SERVICE); 
      Intent i = new Intent(context, MyStartServiceReceiver.class); 
      PendingIntent pending = PendingIntent.getBroadcast(context, 0, i, 
        PendingIntent.FLAG_CANCEL_CURRENT); 
      Calendar cal = Calendar.getInstance(); 
      // Start 30 seconds after boot completed 
      cal.add(Calendar.SECOND, 30); 
      // 
      // Fetch every 30 seconds 
      // InexactRepeating allows Android to optimize the energy consumption 
      service.setInexactRepeating(AlarmManager.RTC_WAKEUP, 
        cal.getTimeInMillis(), REPEAT_TIME, pending); 

      // service.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 
      // REPEAT_TIME, pending); 

     } 

} 

3> 
public class MyStartServiceReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Intent service = new Intent(context, LocalWordService.class); 
     context.startService(service); 
    } 

} 

4> 
public class MyService extends Service { 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     return super.onStartCommand(intent, flags, startId); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

} 

和清單文件如下

<application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="de.vogella.android.ownservice.local.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <service 
      android:name="de.vogella.android.ownservice.local.LocalWordService" 
      android:icon="@drawable/ic_launcher" 
      android:label="@string/service_name" > 
     </service> 

     <service 
      android:name="de.vogella.android.ownservice.local.MyService" 
      android:process=":meinprocess" 
      android:icon="@drawable/ic_launcher" 
      android:label="@string/service_name" > 
     </service> 

     <receiver android:name="de.vogella.android.ownservice.local.MyScheduleReceiver" > 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      </intent-filter> 
     </receiver> 
     <receiver android:name="de.vogella.android.ownservice.local.MyStartServiceReceiver" > 
     </receiver> 
    </application> 

回答

-1

你想從一個類B類用一定的方法?如果是這樣,請將類的初始化與變量放在類的頂部。那麼,在你的OnCreate()方法中,啓動該類的新對象。

EX: // class a initialization Private a mA;

public void OnCreate(Bundle savedInstanceState) 
{ 
    super.OnCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    //Other code 
    mA = new a(); 
} 
+0

不,不。我想創建另一個應用程序,如'AnotherApp',並且我想在'AnotherApp'中使用de.vogella.android.ownservice.local.MyService,如果您看到我所說的清單持有android:process =「:meinprocess」,如果我刪除':'那麼該服務將是一個類似的全球服務。那麼如何在AnotherApp應用程序中使用'MyService'? – 2013-05-02 11:37:07

+0

您是否嘗試過將'MyService'作爲庫添加到您要使用它的項目中?查看Android開發者鏈接瞭解詳情,然後回到我的面前,讓我知道它是否有效! http://developer.android.com/tools/projects/projects-eclipse.html#ReferencingLibraryProject – 2013-05-02 11:44:36

3

您需要公開您的服務,以便其他應用程序可以綁定到它。爲此,請將

android:export="true" 

添加到要共享的服務的清單條目中。

你並不需要把該服務在一個單獨的進程,這樣你就可以刪除

android:process=":meinprocess" 

,除非你想這樣做,因爲其他原因。

8

需要使用一個意圖過濾服務,說爲「LocalWordService」我們在調用應用程序申報

<service 
        android:enabled="true" 
        android:exported="true" 
        android:name="de.vogella.android.ownservice.local.MyService"> 
     <intent-filter> 
      <action android:name="de.vogella.android.ownservice.local.START_SERVICE" /> 
     </intent-filter> 
     </service> 

,我們只需要添加的行獲取服務

Intent intent=new Intent("de.vogella.android.ownservice.local.START_SERVICE"); 
       startService(intent); 

就是這樣。

+1

花了幾個小時後,我解決了我的任務,感謝您的代碼。 – 2015-01-12 16:13:59

+0

我這樣做,我得到一個異常,說意圖需要明確聲明 – Libathos 2016-12-05 12:48:27

相關問題