大哥 你这报错信息呢?
FATAL EXCEPTION: main
后面的信息呢? 没有的话没法解决呀
还有下面这段代码能编译通过吗?
if (togglebutton.isChecked()) {
intent = new Intent();
intent.setAction("com.example.temp.MY_SERVICE");
startService(intent);
} else {
stopService(intent);
}
FATAL EXCEPTION: main
后面的信息呢? 没有的话没法解决呀
还有下面这段代码能编译通过吗?
if (togglebutton.isChecked()) {
intent = new Intent();
intent.setAction("com.example.temp.MY_SERVICE");
startService(intent);
} else {
stopService(intent);
}
Intent intent = new Intent(yourActivity.this,yourService.class);
错误信息如下
intent = new Intent();
intent.setAction("com.example.temp.MY_SERVICE");
startService(intent);方法二:
Intent intent = new Intent(MainActivity.this, MyService.class);
startService(intent);
AndroidManifest.xml
<service android:name="MyService">
<intent-filter >
<action android:name="com.example.temp.MY_SERVICE"/>
</intent-filter>
</service>MyService.java
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {MainActivity.java
package com.example.temp;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;public class MainActivity extends Activity {
... ...
intent = new Intent();
intent.setAction("com.example.temp.MY_SERVICE");
startService(intent);