Error: constructor de intenciones indefinidas al iniciar el servicio en android
Estoy intentando comenzar el servicio en la actividad. Pero muestra error como "el constructor Intent (SampleService, MyService) es undefined"
MyService.java
- ¿Puede Activity.getIntent () devolver alguna vez null?
- ¿Por qué dos parámetros al constructor ComponentName?
- Iniciar siempre una nueva instancia de actividad sin historial
- No se ha encontrado actividad para manejar Intent with action.DIAL
- Solicitar actividad predeterminada sin abrir realmente la actividad
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(); } }
Inicio del servicio desde 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); } }
Servicio inicializado en archivo de manifiesto.
<service android:enabled="true" android:name="com.MyApp.MyService" />
Ayúdame a resolver el error.
- Cómo compartir imágenes en google Plus a través de una aplicación para Android?
- Cómo establecer FileProvider para el archivo en el directorio de caché externo
- Extraer / distinguir el nombre de ActivityAlias de TargetActivity
- Modificar permanentemente el Intent que inició una Actividad
- Devolver el resultado de la intención cuando la actividad se cierra con el botón Atrás
- Titanium android intención externa a la aplicación específica
- Incidencia de selección de Android
- ¿Cómo utilizo la acción de intento USER_PRESENT?
no es Service.this
tienes que pasar clase así cambiar así.
Intent myintent =new Intent(SampleService.this,MyService.Class);
Cambia esto
Intent myintent =new Intent(SampleService.this,MyService.this);
a
Intent myintent =new Intent(SampleService.this,MyService.Class); // first param is a context second param is a class in your case a MyServiceClass
Observe los constructores públicos en que no tiene un constructor como Intent(SampleService, MyService)
. Tiene Intent(SampleService, MyService)
erróneos para el constructor de intenciones.
http://developer.android.com/reference/android/content/Intent.html
public Intent (Context packageContext, Class<?> cls) Added in API level 1 Create an intent for a specific component. All other fields (action, data, type, class) are null, though they can be modified later with explicit calls. This provides a convenient way to create an intent that is intended to execute a hard-coded class name, rather than relying on the system to find an appropriate class for you; see setComponent(ComponentName) for more information on the repercussions of this. Parameters packageContext A Context of the application package implementing this class. cls The component class that is to be used for the intent.
- La conexión a Internet no funciona si mi aplicación se ejecuta en un emulador de API 8
- Android: Implementación de un botón Atrás de una clase