0

ここSOにはこれらの質問がたくさんあることは知っていますが、可能な解決策を探した後、見つけられなかったので、ここで助けていただければ幸いです。

私は3つのクラスファイルを持っています.MainActivityにはチェックボックスがあり、これを押すとブロードキャストレシーバーを持つアクティビティが開始され、ブロードキャストレシーバーはユーザーの場所を確認するサービスを開始することになっています。

コードはかなり大きいので、主要な部分だけを表示しようとしますが、他に何か必要な場合はお知らせください。

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_Info アクティビティ:

     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>

また、私が説明しているこのシナリオでは放送受信機が必ずしも必要ではないことも知っていますが、プロジェクトの要件です。

4

2 に答える 2

0

はい、可能です)したがって、ServiceConnectionオブジェクト(mConnection)とメソッドdoBindService()、doUnbindService()を作成する必要があります。
例えば、

        / **
         *サービスのメインインターフェイスと対話するためのクラス。
         * /
        private ServiceConnection mConnection = new ServiceConnection(){
            public void onServiceConnected(ComponentName className、
                    IBinderサービス){

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()を呼び出す必要があります。

于 2013-01-28T14:16:33.757 に答える
0

現在、コードに基づいて、com.example.systeminfo.LOCATION_READY インテント... をブロードキャストするとサービスが開始されます。このインテントがブロードキャストされると、onRecieve メソッドでサービスが開始されます。

于 2013-01-28T13:59:56.560 に答える