没有任何GUI的Android服务



我想在android没有任何GUI或活动的应用程序。在我的例子中,我将只显示我需要的自定义toast消息。我给我的代码片段,这是显示完成,但没有预期的结果。

Manifest文件是

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

接收类为

    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)); 
        }
    }
}

服务类为

    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;
            }
        }
}

请帮助我,我在哪里犯错误显示自定义吐司??

有点晚了,但有人会发现它很有用:

你错过了:

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

所以你应该有

<intent-filter>

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

<action android:name="MY_INTENT" />

</intent-filter>

你确定MyService已经启动或创建了吗??

当你想要启动服务时,你必须确保MyIntentReceiver是被切换的,有一个方法你可以试试:您可以在Manifest.xml

中设置以下代码
 <receiver android:name=".MyReceiver">
         <intent-filter>
            <action android:name="android.intent.action.AIRPLANE_MODE" />
        </intent-filter>
 </receiver>

运行你的应用;然后将你的设备改为飞行模式,你会发现你的服务被创建了;

当然你可以用另一种方式来切换接收器;

change in manifest::

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

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

最新更新