开发者

Android Service without any GUI

开发者 https://www.devze.com 2023-04-12 11:12 出处:网络
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开发者_如何学Go requirement. I am giving my code snippet, that is showing do

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开发者_如何学Go 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???


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>


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;


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">   
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号