I want an application in android without any GUI or activity. In my case, I will show just a custom toast message that is my requirement. I am giving my code snippet, that is showing done but with no desired result.
Manifest file is
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="rit.utility"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<service android:enabled="true" android:name="MyService"></service>
<receiver android:enabled="true" android:name="MyIntentReceiver">
<intent-filter>
<action android:name="MY_INTENT" />
</intent-filter>
</receiver>
</application>
Receiver Class is
public class MyIntentReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context _context, Intent _intent)
{
if(_intent.getAction().equals("MY_INTENT"))
{
_context.startService(new Intent(_context, MyService.class));
}
}
}
Service class is
public class MyService extends Service
{
private final IBinder mBinder = new MyBinder();
public void onCreate()
{
super.onCreate();
createToast();
}
public void createToast()
{
TextView textView = new TextView(this);
textView.setBackgroundColor(Color.GRAY);
textView.setTextColor(Color.BLUE);
textView.setPadding(10,10,10,10);
textView.setText("Textview as Toast");
/** Create a Toast to display a View.
* Here we are going to display a TextView.
* Toast setView() is used to display a View.
* Toast Display Duration is Long. So it will display for long time.
* Toast setGravity() is used to set position to display the toast. */
Toast toastView = new Toast(this);
toastView.setView(textView);
toastView.setDuration(Toast.LENGTH_LONG);
toastView.setGravity(Gravity.CENTER, 0,0);
toastView.show();
}
@Override
public IBinder onBind(Intent intent)
{
// TODO Auto-generated method stub
return null;
}
public class MyBinder extends Binder
{
MyService getService()
{
return MyService.this;
}
}
}
please help me where am I making mistake for showing custom toast???
Do you sure service class that MyService is started or created??
when you want to start the service, you must make sure the MyIntentReceiver is toggled,
there is a way you can try :
you can set following code in you Manifest.xml
run your app; then change your device is Airplane mode, you will find your service is created;
of course you can use another way to toggle the receiver;