2

バックグラウンドで実行されるサービスとしてアプリを作成しました。このアプリは基本的にバッテリーアラームです。正常に動作しますが、唯一の問題は、このサービスが実行されているときに、アクティブなアプリケーション タスク マネージャーにもこのアプリが表示されることです。そのため、このアプリを終了すると、そのサービスも停止します。だから私が望むのは、ユーザーがアプリ設定のボックスをオフにしたときにのみこのサービスを停止することです. チェックされている場合、アクティブなアプリケーション タスク マネージャーで閉じられていても停止されません。

タスク マネージャーでのアプリの表示を停止するにはどうすればよいですか?

ここでコードを提供する必要があると思いますこれは私のサービスクラスです

public class BatteryService extends Service {
Notify notification = new Notify();
BatteryAlarm alarm = new BatteryAlarm();
private MediaPlayer mMediaPlayer;
boolean flag = false;

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

//method to start service
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    notification.initNotification(this, false);
    this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
    return START_STICKY;
}

//Broadcast receiver to get battery info
private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context c, Intent i) {
        //notification.initNotification(c);

        int level = i.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
        int plugged = i.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);

        SharedPreferences getAlarm = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        String alarms = getAlarm.getString("ringtones", "content://media/internal/audio/media/45"); // /system/media/audio/ringtones/ANDROMEDA.ogg , content://media/internal/audio/media/45
        Uri uri = Uri.parse(alarms);

        if(plugged == 2) {
            if(level == 100) {
                if(uri != null) {
                    if(flag == false) {
                        playAlarm(c, uri);
                        notification.initNotification(c, true);
                        Toast.makeText(c, "Battery charge is completed. Unplug your mobile phone!", Toast.LENGTH_LONG).show();
                        flag = true;
                    }
                }
            }
        } else if (plugged == 0) {
            if(uri != null) {
                stopAlarm();
            }
            notification.cancelNotification(c);
            //Toast.makeText(c, "Mobile is unplugged", Toast.LENGTH_LONG).show();   
        }   
    }
};

//play alarm method
private void playAlarm(Context c, Uri uri) {

    mMediaPlayer = new MediaPlayer();
    try {
        mMediaPlayer.reset();
        mMediaPlayer.setDataSource(getBaseContext(), uri);
        final AudioManager audioManager = (AudioManager) c.getSystemService(Context.AUDIO_SERVICE);
        if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
            mMediaPlayer.prepare();
            mMediaPlayer.start();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        onDestroy();
    }

}

//method to stop playing alarm
private void stopAlarm() {
    mMediaPlayer.stop();
    flag = false;
}

//method to stop service
public void onDestroy() {
    super.onDestroy();
    notification.cancelNotification(this);
    unregisterReceiver(this.mBatInfoReceiver);
    stopAlarm();
    Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
}

}

これが私の主な活動です

public class BatteryNotify extends PreferenceActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.xml.prefs);
    addPreferencesFromResource(R.xml.prefs);

    SharedPreferences getCB = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    boolean cb = getCB.getBoolean("checkbox", true);
    final CheckBoxPreference checkboxPref = (CheckBoxPreference) getPreferenceManager().findPreference("checkbox");

    if(cb == true) {
        startService(new Intent(getBaseContext(), BatteryService.class));
    } else if(cb == false) {
        stopService(new Intent(getBaseContext(), BatteryService.class));
    }

    checkboxPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

        public boolean onPreferenceChange(Preference preference, Object newValue) {
            if(newValue.toString().equals("true")) {
                startService(new Intent(getBaseContext(), BatteryService.class));
            } else {
                stopService(new Intent(getBaseContext(), BatteryService.class));
            }
            return true;
        }
    });

}

}

ここに私のメニフェストファイルがあります

<uses-sdk android:minSdkVersion="10" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".BatteryNotify"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

4

2 に答える 2

2

これを行う最善の方法は、 を作成BroadcastReceiverし、適切な を使用してマニフェストに登録し、intent-filterそれを受け取ると を開始するServiceActivity、必要なタスクを実行することです。

編集:

別のクラスとして作成BroadcastReceiverし、マニフェストに登録します。バッテリー イベントを受け取ると、 を作成してPendingIntentを開始しServiceます。そうすれば、アプリが実行されていなくても問題ありません。それはあなたのために開始されます。

于 2012-05-13T11:14:44.663 に答える
1

タスクマネージャーにアプリを表示しないようにするにはどうすればよいですか?

明らかなセキュリティ上の理由から、できません。

于 2012-05-13T11:10:29.500 に答える