2013-05-02 8 views
5

私はサービスを含むアンドロイドアプリケーションを持っています。今私は別のアプリケーションでそのサービスにアクセスしたいと思います。どうやってやるの?私はネット上でこのアプリケーションを見つけました。コードスニペットは別のアプリケーションでアプリケーションサービスを使用する

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

あなたはクラスAからクラスBに特定のメソッドを使用するために欠けているが、次のようにありますか?そのような場合は、クラスの初期化をクラスの先頭に変数を入れてください。次に、OnCreate()メソッドで、そのクラスの新しいオブジェクトを開始します。

EX: //クラスa初期化 プライベートa mA;

+0

そうではありません。 「AnotherApp」と言う別のアプリケーションを作成したいと思います。私が言った場合は、アンドロイド:process = ":meinprocess"が表示されている場合は、 'AnotherApp'にde.vogella.android.ownservice.local.MyServiceを使用したいと思います。 ':'を削除すると、サービスはそのようなグローバルサービスになります。どうすればAnotherAppアプリケーションで 'MyService'を使うことができますか? –

+0

'MyService'をライブラリとして使用してプロジェクトに追加しようとしましたか?詳細については、Android Developersのリンクを見て、私に連絡して、うまくいけば教えてください! http://developer.android.com/tools/projects/projects-eclipse.html#ReferencingLibraryProject –

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

数時間を過ごした後、私はあなたのコードのおかげで仕事を解決しました。 –

+0

私はこれを行いましたが、意図を明示的に宣言する必要があるという例外があります – Libathos

関連する問題