MediaPlayer Service not working in Android Studio - android

I'm using the following code for MediaPlayer services. But the following service is not running. The toasts are being triggered but the player doesn't play any song.
The 'onStart()' method is deprecated as I override it.
package com.example.user.stringremoval;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;
public class MyService extends Service {
MediaPlayer mediaPlayer;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate()
{
Toast.makeText(this, "Service Created", Toast.LENGTH_SHORT).show();
mediaPlayer=MediaPlayer.create(this,R.raw.bet);
mediaPlayer.setVolume(50,50);
mediaPlayer.setLooping(false);
}
#Override
public void onStart(Intent intent,int startId)
{
Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
mediaPlayer.start();
}
#Override
public void onDestroy()
{
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show();
mediaPlayer.stop();
}
}
Error in Logcat:
06-27 12:47:03.175 3022-3022/com.example.user.stringremoval E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
Activity class:
package com.example.user.stringremoval;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class AudioPlayer extends AppCompatActivity implements View.OnClickListener {
Button bn1,bn2,bn3;
Intent intent1=getIntent();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio_player);
bn1=(Button)findViewById(R.id.button3);
bn2=(Button)findViewById(R.id.button4);
bn3=(Button)findViewById(R.id.button5);
bn1.setOnClickListener(this);
bn2.setOnClickListener(this);
bn3.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.button3:
startService(new Intent(this,MyService.class));
break;
case R.id.button4:
stopService(new Intent(this,MyService.class));
break;
case R.id.button5:
Intent intent=new Intent(this,MainActivity.class);
startActivity(intent);
}
}
}
Android Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.stringremoval">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AudioPlayer"></activity>
<service android:name=".MyService"
android:enabled="true"/>
</application>
</manifest>
Can anyone help me to resolve this problem?

You have to Override onStartCommand() method and return START_STICKY have look this!
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// play mp3 code here
return START_STICKY;
}
refer this link for better understanding of above method and START_STICKY.
Hope it will help you!

Try this code and define below things into your code..
// this line define into activity class into oncrateview method.
startService(new Intent(MyActivity.this, MyService.class));
add below line into android manifest file ..
between application tag..
<service android:enabled="true" android:name="com.my.packagename.MyService" />

Related

Unable to bind / connect to remote service created as part of an AAR library

I have created a service which lives within a library. The library is then included in another project which attempts to launch the service. I am using AIDL to interact however when I attempt to connect to the service my onConnected method is never hit.
From the log files I can see the following error:
D/ActivityManager: bindService callerProcessName:com.something.services.dummyproject, calleePkgName: com.something.services.dummyproject, action: com.something.services.dummyservice.IDummyService
W/ActivityManager: Unable to start service Intent { act=com.something.services.dummyservice.IDummyService cmp=com.something.services.dummyproject/com.something.services.dummyservice.IDummyService } U=0: not found
D/ActivityManager: bindService callerProcessName:com.something.services.dummyproject, calleePkgName: com.something.services.dummyservice.service, action: null
W/ActivityManager: Unable to start service Intent { cmp=com.something.services.dummyservice.service/.DummyService } U=0: not found
The DummyService.java file is as follows:
package com.something.services.dummyservice.services;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import com.something.services.dummyservice.IDummyService;
public class DummyService extends Service {
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
return mBinder;
}
private final IDummyService.Stub mBinder = new IDummyService.Stub() {
#Override
public String dummyHello() throws RemoteException {
return "Hello";
}
#Override
public void exit() throws RemoteException {
stopSelf();
}
};
}
The DummyService Manifest is as follows:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.something.services.dummyservice">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<service
android:name=".services.DummyService"
android:exported="true"
android:label="#string/app_name"
tools:ignore="ExportedService" />
</application>
</manifest>
The IDummyService.aidl file is as follows:
// IDummyService.aidl
package com.something.services.dummyservice;
// Declare any non-default types here with import statements
interface IDummyService {
String dummyHello();
void exit();
}
I'm generating a .aar file with the above.
I've then included that library into my main project file.
The manifest in my main project is as follows:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.something.services.dummyproject">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The MainActivity.java file is as follows:
package com.something.services.dummyproject;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.something.services.dummyservice.IDummyService;
import butterknife.Bind;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity {
private IDummyService mDummyService;
#Bind(R.id.btnStartService)
Button btnStartService;
#Bind(R.id.btnEndService)
Button btnEndService;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
btnStartService.setOnClickListener(startServiceOnClickListener);
btnEndService.setOnClickListener(endServiceOnClickListener);
}
private ServiceConnection mConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName className, IBinder service) {
mDummyService = IDummyService.Stub.asInterface(service);
dummyHello();
}
#Override
public void onServiceDisconnected(ComponentName className) {
mDummyService = null;
}
};
private final View.OnClickListener startServiceOnClickListener = new View.OnClickListener() {
#Override
public void onClick(final View completeButton) {
Intent intent = new Intent(MainActivity.this, com.something.services.dummyservice.IDummyService.class);
intent.setAction(IDummyService.class.getName());
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
};
private final View.OnClickListener endServiceOnClickListener = new View.OnClickListener() {
#Override
public void onClick(final View completeButton) {
exitService();
}
};
private void dummyHello() {
try {
String response = mDummyService.dummyHello();
Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG);
} catch (RemoteException e) {
e.printStackTrace();
}
}
private void exitService() {
try {
if (mDummyService != null) {
mDummyService.exit();
}
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
The dummyProject also contains the exact same AIDL file. In both cases the AIDL file lives in an aidl folder which lives alongside the src\JAVA folder. That contains a package folder which contains the AIDL file.
Any help with identifying why I'm unable to connect to the service is very much appreciated!

how to run my application above keyboard when user click on any textView in any application?

I would like to visible my application view above the keyboard whenever the user clicks on textView in any application, the only thing that I knew is my application should be running in background and make the view visible when keyboard gets called, but I'm not sure if that's possible and if it's, I don't know how to do that.
I hope that helps you to get what I'm talking about
Edit 8/5/2016:
I create a foreground service but I don't know how to detect soft keyboard in the other apps
MainActivity.java
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent SERVICE_INTENT = new Intent(this,MyServiceTest.class);
startService(SERVICE_INTENT);
}
}
MyServiceTest.java
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
public class MyServiceTest extends Service {
#Override
public void onCreate() {
super.onCreate();
Intent MAIN_INTENT = new Intent(this,MainActivity.class);
PendingIntent myPendingIntent = PendingIntent.getActivity(this,0,MAIN_INTENT,0);
Notification myNotification = new Notification.Builder(this).setContentTitle("Title")
.setContentText("The Content text").setSmallIcon(R.mipmap.ic_launcher)
.setWhen(System.currentTimeMillis()).setContentIntent(myPendingIntent)
.build();
startForeground(1337,myNotification);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Thread myThread = new Thread(new Runnable() {
#Override
public void run() {
}
});
myThread.start();
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity"
android:windowSoftInputMode="stateHidden|adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyServiceTest">
</service>
</application>
Any information or idea could be helpful, Thanks in advance ^_^
I believe this is what you want:
android:windowSoftInputMode="stateHidden|adjustPan"
Add it to your activity in your manifest.
Example:
<activity
android:name="activities.WorkObjectActivity"
android:windowSoftInputMode="stateHidden|adjustPan"//This one
android:label="#string/urenToevoegen"
android:screenOrientation="portrait"
android:parentActivityName="activities.MainActivity"/>

Always running Service with BroadcastReceiver for Call Tapping

My Requirment
I am trying to build an application which shows a Toast whenever user makes a outgoing call from the phone. For this I am using a BroadcastReceiver to tap the call action and a service (to run Receiver always). once I start this activity, it starts showing toast when a outgoing call get initiated ...everything works well.
Issues
Sometimes I dont get any Toast Action EVEN for Outgoing call as well, is that mean service stops after some time ?
After phone reboot Toast Action for Outgoing call stops. Untill you start the servce again manually.
The code I have written is ok ? OR can it be improved ?
Below is the complete code -
MainActivity.class
public class MainActivity extends Activity
{
CallNotifierService m_service;
boolean isBound = false;
private ServiceConnection m_serviceConnection = new ServiceConnection()
{
#Override
public void onServiceConnected(ComponentName className, IBinder service)
{
m_service = ((CallNotifierService.MyBinder)service).getService();
Toast.makeText(MainActivity.this, "Service Connected", Toast.LENGTH_LONG).show();
isBound = true;
Intent intent = new Intent(MainActivity.this, CallNotifierService.class);
startService(intent);
}
#Override
public void onServiceDisconnected(ComponentName className)
{
m_service = null;
isBound = false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, CallNotifierService.class);
bindService(intent, m_serviceConnection, Context.BIND_AUTO_CREATE);
}
.
.
.
}
CallNotifierService.class
public class CallNotifierService extends Service
{
private final IBinder myBinder = new MyBinder();
private static final String ACTION_OUTGOING_CALL = "android.intent.action.NEW_OUTGOING_CALL";
private static final String ACTION_ANSWER = "android.intent.action.ANSWER";
private static final String ACTION_CALL = "android.intent.action.CALL";
private CallBr br_call;
#Override
public IBinder onBind(Intent arg0)
{
return myBinder;
}
#Override
public void onDestroy()
{
Log.d("service", "destroy");
this.unregisterReceiver(this.br_call);
Toast.makeText(CallNotifierService.this, "Receiver Un-Registered", Toast.LENGTH_LONG).show();
super.onDestroy();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
final IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_ANSWER);
filter.addAction(ACTION_CALL);
filter.addAction(ACTION_OUTGOING_CALL);
this.br_call = new CallBr();
this.registerReceiver(this.br_call, filter);
Toast.makeText(CallNotifierService.this, "onStartCommand Called", Toast.LENGTH_LONG).show();
return START_STICKY;
}
public class MyBinder extends Binder
{
CallNotifierService getService()
{
return CallNotifierService.this;
}
}
public class CallBr extends BroadcastReceiver
{
public CallBr() {}
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Action:"+intent.getAction(), Toast.LENGTH_LONG).show();
}
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alwaysrunningprocesswithcallanswertap"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-sdk
android:minSdkVersion="22"
android:targetSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.example.alwaysrunningprocesswithcallanswertap.CallNotifierService" />
</application>
</manifest>
Could anyone please help for the issue OR pointout something better ?
#Bharat Once device switch off automatically service will stop, So when ever your device reboot that time you need to start service again then only you will get Toast Action, refer this example code :
BootCompleteReceiver.java
package com.example.newbootservice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootCompleteReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, AutostartService.class);
context.startService(service);
}
}
AutostartService .java
package com.example.newbootservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class AutostartService extends Service {
#Override
public void onCreate() {
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
return Service.START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroy", Toast.LENGTH_LONG).show();
}
#Override
public IBinder onBind(Intent arg0) {
return null;
}
}
MainActivity.java
package com.example.newbootservice;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startService(new Intent(getBaseContext(), AutostartService.class));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.newbootservice"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<service android:name=".AutostartService"/>
<receiver android:name=".BootCompleteReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Google Activity Recognition API

I am going to write an application using google play services activity recognition api. the training in android developer site was straight forward but in the past couple of hours i wrote a simple application but i can't get any result from it.
UPDATE : Actually i am going to show the current activity of user in 5 second intervals as a toast message (as you can see in OnIntentHandler method in ActivityRecognitionService Intent Servie) . i think something is wrong with the calling the Intent because as you can see in my code the toast that says the ActivityRecognitionClient is connected on OnConnected method.
did i miss something ?
Thanks in advance.
Manifest File :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nikapps.activityrecognition"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission
android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<service
android:name="com.nikapps.activityrecognition.ActivityRecognitionService"
android:label="#string/app_name"
android:exported="false">
</service>
<activity
android:name="com.nikapps.activityrecognition.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
package com.nikapps.activityrecognition;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.location.ActivityRecognitionClient;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends FragmentActivity implements ConnectionCallbacks, OnConnectionFailedListener{
public static int intervals = 5000;
private PendingIntent pendingIntent;
private ActivityRecognitionClient activityRecognition;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activityRecognition = new ActivityRecognitionClient(this, this, this);
activityRecognition.connect();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onConnectionFailed(ConnectionResult result) {
// TODO Auto-generated method stub
}
#Override
public void onConnected(Bundle connectionHint) {
// TODO Auto-generated method stub
Toast.makeText(this, "connected", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, ActivityRecognitionService.class);
pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
activityRecognition.requestActivityUpdates(0, pendingIntent);
}
#Override
public void onDisconnected() {
// TODO Auto-generated method stub
Toast.makeText(this, "disconnected", Toast.LENGTH_SHORT).show();
}
}
ActivityRecognitionService.java
package com.nikapps.activityrecognition;
import com.google.android.gms.location.ActivityRecognitionResult;
import com.google.android.gms.location.DetectedActivity;
import android.app.IntentService;
import android.content.Intent;
import android.widget.Toast;
public class ActivityRecognitionService extends IntentService{
public ActivityRecognitionService() {
super("ActivityRecognitionService");
Toast.makeText(this, "here", Toast.LENGTH_SHORT).show();
// TODO Auto-generated constructor stub
}
#Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(this, "here2", Toast.LENGTH_SHORT).show();
ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
DetectedActivity activity = result.getMostProbableActivity();
int type = activity.getType();
Toast.makeText(this, getNameFromType(type), Toast.LENGTH_SHORT).show();
}
private String getNameFromType(int activityType) {
switch(activityType) {
case DetectedActivity.IN_VEHICLE:
return "in_vehicle";
case DetectedActivity.ON_BICYCLE:
return "on_bicycle";
case DetectedActivity.ON_FOOT:
return "on_foot";
case DetectedActivity.STILL:
return "still";
case DetectedActivity.UNKNOWN:
return "unknown";
case DetectedActivity.TILTING:
return "tilting";
}
return "unknown";
}
}
You can broadcast an intent to send messages from the service to the main class
For example, use the following in your ActivityRecognitionService.java:
Intent mIntent = new Intent("myCustomIntentMessage")
.putExtra("ActivityType", getNameFromType(type));
getLocalBroadcast().sendBroadcast(mIntent);
Then you just need to register the broadcast receiver for "myCustomIntentMessage" on your MainActivity.java and put the Toast message code in the broadcast receiver OnReceive event.
You can only call the Toast from a thread that has a handler/Looper set up. To achieve what you're envisioning you have two options
Use a broadcast from the IntentService and have an activity that has a BroadcastReceiver that shows the UI. This way, the toast is not shown when the activity is not in the foreground . register/unregister the receiver in your onResume()/onPause()
create a handler and call your method in Handler.post(new Runnable() {.. })
Error calling toast from Service Android

Issue in Calling Service

Here is my service class
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class Communicationservice extends Service {
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
super.onCreate();
Log.d("I AM Service","Service Created");
}
#Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
}
}
Here is my Main Activity
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("flow","1");
Intent in = new Intent("com.yal.Communication.Communicationservice");
this.startService(in);
//startService(new Intent(MainActivity.this,Communicationservice.class));
Log.d("flow","2");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Here is Mainfest.xml
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity > ... </activity>
<service android:enabled="true" android:name="com.yal.Communication.Communicationservice"></service>
</application>
Error
11-02 16:34:17.939: D/flow(1390): 1
11-02 16:34:17.939: W/ActivityManager(92): Unable to start service Intent { act=com.yal.Communication.Communicationservice }: not found
11-02 16:34:17.939: D/flow(1390): 2
you need to check Intent-Filter.
below snippet will help you.
<service android:name="com.yal.Communication.Communicationservice">
<intent-filter>
<action android:name="com.yal.Communication.Communicationservice" />
</intent-filter>
</service>
Then you can call this services like follows.
Intent serviceIntent = new Intent("com.yal.Communication.Communicationservice");
startService(serviceIntent);
Constructor public Intent (String action) can be used to specify action only see following java doc:
public Intent (String action)
Create an intent with a given action. All other fields (data, type, class) are null. Note that the action must be in a namespace because Intents are used globally in the system -- for example the system VIEW action is android.intent.action.VIEW; an application's custom action would be something like com.google.app.myapp.CUSTOM_ACTION.
Parameters
action The Intent action, such as ACTION_VIEW.
make following change to you intent creation:
Intent in = new Intent(this,com.yal.Communication.Communicationservice.class);
Or specify the action in the me manifest file:
<service android:name="com.yal.Communication.Communicationservice">
<intent-filter>
<action android:name="com.yal.Communication.Communicationservice" />
</intent-filter>
</service>

Categories

Resources