Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
while starting a new service i have used

startService(new Intent(this, ser.class);

n m getting error in log cat unable to start intent in android.


demoseractivity.java

Java
package com;

import com.demoser.R;
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 demoserActivity extends Activity implements OnClickListener {
	private static final String TAG = "ServicesDemo";
	  Button buttonStart, buttonStop;

	  @Override
	  public void onCreate(Bundle savedInstanceState) {
		  super.onCreate(savedInstanceState);
	    setContentView(R.layout.main);

	    buttonStart = (Button) findViewById(R.id.buttonStart);
	    buttonStop = (Button) findViewById(R.id.buttonStop);

	    buttonStart.setOnClickListener(this);
	    buttonStop.setOnClickListener(this);
	  }

	  public void onClick(View src) {
	    switch (src.getId()) {
	    case R.id.buttonStart:
	      Log.d(TAG, "onClick: starting srvice");
	      startService(new Intent(this, ser.class));
	      break;
	    case R.id.buttonStop:
	      Log.d(TAG, "onClick: stopping srvice");
	      stopService(new Intent(this, ser.class));
	      break;
	    }
	  }
	}


ser.java

Java
package com;

import com.demoser.R;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import android.R.layout.*;
import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

public class ser extends Service

{
	private static final String TAG = "MyService";
MediaPlayer player;
@Override
public IBinder onBind(Intent intent) {
return null;
}

public void onCreate(Bundle savedInstanceState) {
    super.onCreate();
  //  setWallpaper(R.drawable.ic_launcher);
	Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
	Log.d(TAG, "onCreate");
	player = MediaPlayer.create(this, R.raw.demo);
	player.setLooping(false); // Set looping
}

	
@Override
public void onDestroy() {
	Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
	Log.d(TAG, "onDestroy");
	player.stop();
}

public void onStartCommand(Intent intent, int startid) {
	Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
	Log.d(TAG, "onStart");
	player.start();
}
}

main.xml just has two buttons

and manifest has

service enabled="true" android:name=".ser"

inside application tag
Posted
Updated 22-Nov-12 20:16pm
v4

1 solution

Need to describe more about your error.It may be a problem of your ser.class.However,see from here how to use an intent..
http://www.vogella.com/articles/AndroidIntent/article.html[^]
 
Share this answer
 
Comments
Member 9239619 22-Nov-12 21:40pm    
hii ll post my code so u can understand
ridoy 23-Nov-12 2:28am    
try startActivity(new Intent(this, ser.class)); instead of startService(new Intent(this, ser.class));..
Member 9239619 23-Nov-12 3:25am    
do we need to add any permissions??
ridoy 23-Nov-12 7:54am    
if your application requires internet connection then of course you need to add it..
<uses-permission android:name="android.permission.INTERNET">
in AndroidManifest.xml
Member 9239619 24-Nov-12 3:40am    
no it just has to toast a message that service started .. no internet or any thing

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900