从活动控制现有服务的问题

时间:2011-09-22 10:15:27

标签: android service android-activity bind

我是Android新手。我正在Phone Boot Up上开始服务。我想从稍后发布的活动中控制此服务。但是我无法这样做。

是否因为我从BroadCastReceiver启动服务而且上下文存在一些问题?你能让我知道我做错了什么以及我该怎么做。如果有人能解释如何处理这个问题,我将不胜感激。

以下是我的文件:

MyPhoneBoot.java

package phone.Boot;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyPhoneBoot extends android.content.BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent){
    Log.d("MyPhoneBoot", "Phone Boot Captured");
    Intent expIntent=new Intent(context,MyService.class);
    context.startService(expIntent);
    }

}

MyService.Java

package phone.Boot;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service {

public static String TAG="MyNewService";
public static int service_running=999;

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

@Override
public void onCreate() {
    service_running=2;
    Log.d(TAG, "onCreate");
    Log.d(TAG, "Value " + MyService.service_running);
}
@Override
public void onDestroy() {
    service_running=(-2);
    Log.d(TAG, "onDestroy");
    Log.d(TAG, "Value " + MyService.service_running);
}

@Override
public void onStart(Intent intent, int startid) {
    service_running=1;
    Log.d(TAG, "Value " + MyService.service_running);
    Log.d(TAG, "Service has been started!");
}
public void onStop(Intent intent, int startid) {
    service_running=(-1);
    Log.d(TAG, "Value " + MyService.service_running);
    Log.d(TAG, "Service has been stopped!");
}

}

MyActivity.Java

package phone.Boot;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MyActivity extends Activity implements OnClickListener{

@Override
public void onCreate(Bundle savedInstanceState) {
    Log.d("MyActivity", "MyActivity running!" + MyService.service_running);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button myButton, myButton2;
    myButton = (Button) this.findViewById(R.id.button_1);
    myButton.setOnClickListener(this);
    myButton2 = (Button) this.findViewById(R.id.button_2);
    myButton2.setOnClickListener(this);

    /* changes done for binding service */
   }
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch(arg0.getId()){
        case R.id.button_1:
            switch (MyService.service_running)
            {
             case 2:
                Log.d("MyActivity", "Starting already created service!");
                Toast.makeText(this, "Alrdy created -> Starting now!", Toast.LENGTH_SHORT).show();
                startService(new Intent(this,MyService.class));
                break;

             case 1:
                Log.d("MyActivity", "Trying to start existing service!");
                Toast.makeText(this, "Alrdy Running!", Toast.LENGTH_LONG).show();
                break;

             case -2:
             case 999:
                Log.d("MyActivity", "Re-Starting Service");
                Toast.makeText(this, "Re-Starting Service!", Toast.LENGTH_SHORT).show();
                startService(new Intent(this,MyService.class));
                break;
            }

            break;
        case R.id.button_2:
            if(MyService.service_running == -1 || MyService.service_running == -2 || MyService.service_running == 999)
            {
                Log.d("MyActivity", "No Service To Stop!");
                Toast.makeText(this, "Nothing to Stop! x-(", Toast.LENGTH_LONG).show();
            }
            else if(MyService.service_running == 1 || MyService.service_running == 2)
            {
                Log.d("MyActivity", "Halting Service!!");
                Toast.makeText(this, "Stopping Service!", Toast.LENGTH_LONG).show();
                stopService(new Intent(this,MyService.class));
            }
            break;
        }

    }


}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="phone.Boot"
  android:versionCode="1"
  android:versionName="1.0">


<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name="MyPhoneBoot">
<intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED">
    </action>
</intent-filter>
</receiver>
<service android:enabled="true" android:name=".MyService" />
<activity android:name="phone.Boot.MyActivity"
          android:label="@string/app_name">
  <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
</manifest>

2 个答案:

答案 0 :(得分:0)

你究竟想要什么并不清楚。您是要停止服务还是想再次从活动中启动服务,而您却无法获得所需的结果。这post可能会对您有所帮助。

答案 1 :(得分:0)

我也是一个noobie,但我创造了类似于你的东西,它的效果很好。 首先,由于服务和活动是同一项目的一部分,因此您可以从活动中启动和停止服务。在启动时启动并没有什么区别(我停止并从我的活动中启动我的启动服务)。

有几件事:你的意思是“它不起作用”? 应用程序崩溃了吗?当您尝试从活动中访问service_running时,您获得了什么价值?

另外,我对服务生命周期不太了解。虽然您没有明确地停止它,但它只运行一次代码。该服务早在您完成启动过程并访问您的活动时就完成了代码。如果你不能访问service_running变量,我猜这个服务已经死了,它已经不存在了,虽然我可能错了 - 毕竟我是个菜鸟: - )

尝试创建重复性服务(如将永远运行的GPS监听器),并查看是否能使其保持活动状态。