为什么服务没有启动?
Why Service is not starting?
我有 MainActivity 和自定义服务名称 BatteryService.In 此服务我正在测量电池电量并对其进行一些处理。这是此服务的示例代码 class。
public class BatteryService extends Service {
Context context ;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
context = this;
Log.v("Service Started : ", "" );
BatteryService.this.registerReceiver(brodcastReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
protected BroadcastReceiver brodcastReceiver = new BroadcastReceiver() {
int intlevel;
MediaPlayer mediaPlayer;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
intlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
String dtr;
dtr = String.valueOf(intlevel);
Toast.makeText(context, dtr, Toast.LENGTH_LONG).show();
try {
intlevel = intent.getIntExtra("level", 0);
Log.v("Battery level is : ", "" + dtr);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
String strStatus;
if ((status == BatteryManager.BATTERY_STATUS_CHARGING) && (intlevel >= 90)) {
Toast.makeText(context, "charging", Toast.LENGTH_LONG).show();
mediaPlayer = MediaPlayer.create(BatteryService.this, R.raw.alarmsound);
mediaPlayer.start();
}
if ((status == BatteryManager.BATTERY_STATUS_DISCHARGING) && (intlevel >= 99))
{
mediaPlayer.stop();
}
}
};
}
And in Main Activity I have button and on that button click I am
starting service such as :
public class MainActivity extends Activity {
Button btnStart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStart = (Button) findViewById(R.id.btn_startService);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v("Starting service","On Button click");
Intent intent = new Intent(MainActivity.this, BatteryService.class);
startService(intent);
}
});
}
}
并按照以下方式在清单中声明它
android:name="com.brokenfloorstudios.eyebattery.BatteryService" />
我发布这个答案是为了如果有人遇到与我遇到的问题相同的问题。这是一个小的。当我制作自己的自定义服务时,我在 Manifest 文件中的 Application 结束标记下方声明它。
它应该在 manifest.It 中的应用程序标记中声明的位置花了一些时间来弄清楚,因为我在 Logcat 中没有收到任何错误或异常。
试试这个
Intent a = new Intent();
a.setAction (" *** insert service name here**");
StartService(a);
我通常将清单文件中的字符串复制并粘贴到我的 activity,因为它很容易出现拼写错误
我有 MainActivity 和自定义服务名称 BatteryService.In 此服务我正在测量电池电量并对其进行一些处理。这是此服务的示例代码 class。
public class BatteryService extends Service {
Context context ;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
context = this;
Log.v("Service Started : ", "" );
BatteryService.this.registerReceiver(brodcastReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
protected BroadcastReceiver brodcastReceiver = new BroadcastReceiver() {
int intlevel;
MediaPlayer mediaPlayer;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
intlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
String dtr;
dtr = String.valueOf(intlevel);
Toast.makeText(context, dtr, Toast.LENGTH_LONG).show();
try {
intlevel = intent.getIntExtra("level", 0);
Log.v("Battery level is : ", "" + dtr);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
String strStatus;
if ((status == BatteryManager.BATTERY_STATUS_CHARGING) && (intlevel >= 90)) {
Toast.makeText(context, "charging", Toast.LENGTH_LONG).show();
mediaPlayer = MediaPlayer.create(BatteryService.this, R.raw.alarmsound);
mediaPlayer.start();
}
if ((status == BatteryManager.BATTERY_STATUS_DISCHARGING) && (intlevel >= 99))
{
mediaPlayer.stop();
}
}
};
}
And in Main Activity I have button and on that button click I am starting service such as :
public class MainActivity extends Activity {
Button btnStart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStart = (Button) findViewById(R.id.btn_startService);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v("Starting service","On Button click");
Intent intent = new Intent(MainActivity.this, BatteryService.class);
startService(intent);
}
});
}
}
并按照以下方式在清单中声明它
android:name="com.brokenfloorstudios.eyebattery.BatteryService" />
我发布这个答案是为了如果有人遇到与我遇到的问题相同的问题。这是一个小的。当我制作自己的自定义服务时,我在 Manifest 文件中的 Application 结束标记下方声明它。 它应该在 manifest.It 中的应用程序标记中声明的位置花了一些时间来弄清楚,因为我在 Logcat 中没有收到任何错误或异常。
试试这个
Intent a = new Intent();
a.setAction (" *** insert service name here**");
StartService(a);
我通常将清单文件中的字符串复制并粘贴到我的 activity,因为它很容易出现拼写错误