没有任何 GUI 的 Android 服务

发布于 2024-12-08 21:21:36 字数 2620 浏览 0 评论 0原文

我想要一个没有任何 GUI 或活动的 Android 应用程序。就我而言,我将仅显示我的要求的自定义 toast 消息。我给出了我的代码片段,显示已完成但没有预期的结果。

清单文件是

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

}

请帮助我在哪里显示自定义 toast 时出错了???

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小镇女孩 2024-12-15 21:21:36

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

你缺少:

所以你应该有

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>

牵你的手,一向走下去 2024-12-15 21:21:36

您确定 MyService 的服务类已启动或创建吗?

当您要启动服务时,必须确保 MyIntentReceiver 已切换,
有一种方法你可以尝试:
您可以在 Manifest.xml 中设置以下代码

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

运行您的应用程序;然后将您的设备更改为飞行模式,您会发现您的服务已创建;

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

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;

疾风者 2024-12-15 21:21:36

清单更改 ::

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

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">   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文