Android application without GUI

折月煮酒 提交于 2019-12-11 03:59:40

问题


I have been developing a simple application without UI using broadcast receiver.

The app doesn't contain any ACTIVITIES.

I have given necessary permissions.

I took the code from this url:http://developerandro.blogspot.in/2013/09/check-internet-connection-using.html

The app shows a toast "Not connected to internet" when I click change wifi state. It's working correctly.

But my question is There is an activity registered in my manifest file which I don't have. So I delete those lines from my manifest. Then no toasts are shown and I checked the logs too. No output on changing wifi state.

Why this happened? Please help me guys...

Here is the manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.broadcast_internetcheck"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.broadcast_internetcheck.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name="com.example.broadcast_internetcheck.NetworkChangeReceiver"
            android:label="NetworkChangeReceiver" >
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

Here is my Broadcastreceiver class:

public class NetworkChangeReceiver extends BroadcastReceiver{

  @Override
     public void onReceive(final Context context, final Intent intent) {

         String status = NetworkUtil.getConnectivityStatusString(context);
          /*Above line will return the status of wifi */
         Toast.makeText(context, status, Toast.LENGTH_LONG).show();

     }
}

回答1:


You will need to create a dummy activity for a Service which will be triggered in the onCreate() of the dummy, maybe a non-UI with finish() .

Without that the required implementation is not possible, esp above Android 3.1.

http://developer.android.com/about/versions/android-3.1.html#launchcontrols
Run only a background service when application start
Start android application without activity
http://commonsware.com/blog/2011/07/13/boot-completed-regression-confirmed.html

And for more on Service:
http://developer.android.com/guide/components/services.html
https://developer.android.com/training/run-background-service/create-service.html
http://www.vogella.com/tutorials/AndroidServices/article.html




回答2:


You can use service instead. But showing Toast through service bit complicated instead you can show notification through service for No Internet Connection.




回答3:


If you don't want any activity check this answer. Actually you would have to create service for this: link




回答4:


Create a transparent activity. Launch the toast while the activity is active and immediately finish the activity.



来源:https://stackoverflow.com/questions/21772458/android-application-without-gui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!