Ram*_*Ram 3 android android-intent
我尝试在activity中启动服务.但它显示错误,如"构造函数Intent(SampleService,MyService)未定义"
MyService.java
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
public static boolean isInstanceCreated() {
return instance != null;
}
@Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
instance = this;
}
@Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
instance = null;
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(getBaseContext(), "Service started",Toast.LENGTH_SHORT).show();
}
}
Run Code Online (Sandbox Code Playgroud)
从SampleService.java启动服务
public class SampleService extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_activity);
Intent myintent =new Intent(SampleService.this,MyService.this);//Error show here..
startService(myintent);
}
}
Run Code Online (Sandbox Code Playgroud)
清单文件中初始化的服务.
<service android:enabled="true" android:name="com.MyApp.MyService" />
Run Code Online (Sandbox Code Playgroud)
帮我解决错误.
不是Service.this你必须通过课程所以改变这样..
Intent myintent =new Intent(SampleService.this,MyService.Class);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
571 次 |
| 最近记录: |