0

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???

Robert Smith
  • 457
  • 2
  • 9
  • 25
  • check my answer here for an autostart Application, it will help you out http://stackoverflow.com/questions/7690350/android-start-service-on-boot/7690600#7690600 – Lalit Poptani Oct 11 '11 at 05:16
  • Thank you, your link was very very useful. I can run my service now. There was a small mistake in my work. My receiver class was expecting it's package path. thanks a lot :-) – Robert Smith Oct 11 '11 at 07:30

3 Answers3

1

somewhat late but somebody can find it usefull:

You are missing:

<category android:name="android.intent.category.DEFAULT" />

so you should have

<intent-filter>

<category android:name="android.intent.category.DEFAULT" />

<action android:name="MY_INTENT" />

</intent-filter>

zajac.m2
  • 1,218
  • 14
  • 13
0

change in manifest ::

<service android:enabled="true" android:name="MyService"></service> 
    <receiver android:enabled="true" android:name="MyIntentReceiver">   

to

<service android:enabled="true" android:name=".MyService"></service> 
    <receiver android:enabled="true" android:name=".MyIntentReceiver">   
Nikunj Patel
  • 21,853
  • 23
  • 89
  • 133
  • thanks for your reply, but still the application is not showing any toast message, which is to be expected after service is started. I think service is not being started. I want to know how do I start the service? – Robert Smith Oct 10 '11 at 10:00
  • no, I am getting no error. But tell me one thing, is this possible to do anything in android which does not have any GUI or activity? The simple application will serve as utility class. I hope you can understand what I want. – Robert Smith Oct 10 '11 at 10:11
0

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

 <receiver android:name=".MyReceiver">
         <intent-filter>
            <action android:name="android.intent.action.AIRPLANE_MODE" />
        </intent-filter>
 </receiver>

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;

Mr.Jie
  • 26
  • 5
  • that is my question. My service class was named 'MyService' that you see in code snippet and that was supposed to be started by broadcastreceiver class. I debugged broadcast receiver class, but code flow was not reaching at entry point where it could have strted service. I am totally new in it, can you please suggest if there is any possibility to start a service from broadcast receiver class using no activity like in my coding. – Robert Smith Oct 11 '11 at 05:16
  • I change my answer, this website i am unfamiliar; – Mr.Jie Oct 13 '11 at 09:31