Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从广播接收器问题启动android服务_Android_Service_Android Activity_Broadcastreceiver - Fatal编程技术网

从广播接收器问题启动android服务

从广播接收器问题启动android服务,android,service,android-activity,broadcastreceiver,Android,Service,Android Activity,Broadcastreceiver,我知道这里有很多这样的问题,但是在寻找可能的解决方案之后,我没有找到一个,所以这里的任何帮助都是非常感谢的 我有3个类文件,我的MainActivity带有一个复选框,当按下该复选框时,启动一个具有广播接收器的活动,我的广播接收器应该启动一个服务来检查用户位置 代码非常大,所以我将尝试只显示主要部分,如果需要其他内容,请让我知道 在主要活动中: Public void onCheckboxClicked (View view) { boolean checked =

我知道这里有很多这样的问题,但是在寻找可能的解决方案之后,我没有找到一个,所以这里的任何帮助都是非常感谢的

我有3个类文件,我的MainActivity带有一个复选框,当按下该复选框时,启动一个具有广播接收器的活动,我的广播接收器应该启动一个服务来检查用户位置

代码非常大,所以我将尝试只显示主要部分,如果需要其他内容,请让我知道

在主要活动中:

     Public void onCheckboxClicked (View view) {
         boolean checked = ((CheckBox) view).isChecked();
     Switch (view. getId ()) {
        case R.id.checkbox_gps:
            if (checked){
             Intent intent = new Intent(this, GPS_Info.class);
             startActivity(intent);
            }
在GPS_信息活动中:

     public class GPS_Info extends Activity{
          private TextView latitude;
          private TextView longitude;

     @Override
     protected void onCreate(Bundle savedInstanceState){
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_gps_info);
     }

     private BroadcastReceiver GpsReceiver = new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent){
             System.out.println("Broadcast receiver");
             intent.setAction("com.example.systeminfo.GpsTracker");
             context.startService(intent);
        }
      };
我想我的问题是,我可以(如果是,那么如何)启动广播接收器中另一个类文件中的GpsTracker服务吗?我需要发送什么样的意向?我还没有完全掌握意图和上下文的概念

这是我的AndroidManifest.xml:

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

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.systeminfo.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.systeminfo.GpsReceiver" >
            <intent-filter>
                <action android:name="com.example.systeminfo.LOCATION_READY" />

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

        <activity
            android:name="com.example.systeminfo.GPS_Info"
            android:label="@string/title_activity_gps__info"
            android:parentActivityName="com.example.systeminfo.MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.systeminfo.MainActivity" />
        </activity>
        <activity
            android:name="com.example.systeminfo.BatteryStatus"
            android:label="@string/title_activity_battery_status"
            android:parentActivityName="com.example.systeminfo.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.systeminfo.MainActivity" />
        </activity>
        <activity
            android:name="com.example.systeminfo.General_Info"
            android:label="@string/title_activity_general__info"
            android:launchMode="singleInstance"
            android:parentActivityName="com.example.systeminfo.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.systeminfo.MainActivity" />
        </activity>

        <service
            android:name=".GpsTracker"
            android:enabled="false"
            android:exported="false" >
        </service>

    </application>
</manifest>


我还知道,我正在描述的这个senario并不完全需要广播接收器,但这是该项目的一项要求。

现在根据您的代码,该服务将在您广播时启动 com.example.systeminfo.LOCATION\u READY intent,当广播此意图时,OnReceive方法中的服务将启动

是,这是可能的) 因此,您必须创建ServiceConnection对象(mConnection)和方法doBindService(),doUnbindService()
例如,
/** * Class for interacting with the main interface of the service. */ private ServiceConnection mConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) {

            mCallbackText.setText("Attached...");

            // We want to monitor the service for as long as we are
            // connected to it.
            try {

            } catch (RemoteException e) {  }

        }

        public void onServiceDisconnected(ComponentName className) {
            mCallbackText.setText("Disconnected...");
        }
    };

    // connect to service
    void doBindService() {

        Intent intent = new Intent(this, MyServer.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT);

        mCallbackText.setText("Binding...");
    }

    void doUnbindService() {
            // Detach our existing connection.
            unbindService(mConnection);
            mCallbackText.setText("No Service...");
    }
要启动服务,必须调用doBindService(),以关闭-doUnbindService()