我有一个包含服务的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>
答案 0 :(得分:7)
需要使用意图过滤器进行服务,例如我们声明的“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>
在调用app时,我们只需要添加获取服务的行
Intent intent=new Intent("de.vogella.android.ownservice.local.START_SERVICE");
startService(intent);
就是这样。
答案 1 :(得分:3)
您需要公开提供服务,以便其他应用程序可以绑定到该服务。为此,添加
android:export="true"
到您要共享的服务的清单条目。
您无需将该服务放在单独的流程中,因此您可以删除
android:process=":meinprocess"
除非你出于其他原因想要这样做。
答案 2 :(得分:-1)
你想在a类的b级中使用某种方法吗?如果是这样,请将类的初始化与变量放在类的顶部。然后,在您的OnCreate()方法中,启动该类的新对象。
EX: //类初始化 私有a mA;
public void OnCreate(Bundle savedInstanceState)
{
super.OnCreate(savedInstanceState);
setContentView(R.layout.main);
//Other code
mA = new a();
}