2012-05-13 4 views
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; 
     } 
    }); 

} 

} 

で、私のmenifestファイルがある私は、コードを提供するべきだと思う

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

+0

どのようにサービスを開始していますか? – techiServices

+0

サービスはマニフェストに登録されていますか?アクティビティは、サービスの制御(「開始」と「停止」)を「ただ」処理する必要がありますが、そのコードは含まれていません。 – yoshi

+0

この点について:FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS? http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS – moujib

答えて

1

は、どのように私は、タスクマネージャで私のアプリの表示を停止することができますか?

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

+0

CommonsWareでは、Gmailやその他のアプリはどのように動作しますか?タスクマネージャにも表示されませんが、バックグラウンドで実行されています。私が正しくない場合は、私を修正してください。 – 2619

+0

@ al0neevenings:それはあなたの "タスクマネージャー"を書いた人のための質問でしょう。あなたのアプリケーションのいずれかが実行されている場合、 "タスクマネージャー"がそれを検出し、あなたのリストを選ぶことができると言っても過言ではありません。利用可能な「タスクマネージャー」アプリはおそらく1000台あり、それらはすべて異なる方法で動作します。 – CommonsWare

2

これを行うための最善の方法は、それはそれはあなたが必要なものは何でもタスクを実行するためにServiceまたはActivityを開始し1を受信したとき、BroadcastReceiverを作成し、適切なintent-filter秒でマニフェストに登録してすることです。

EDIT:

は別々のクラスとしてごBroadcastReceiverを作成し、マニフェストに登録。バッテリーイベントを受信したら、PendingIntentを作成してServiceを開始します。アプリが実行されていないかどうかは関係ありません。それはあなたのために始まります。

+0

あなたが 'PendingIntent'(私のような^、^)に直面したことがないなら、公式のドキュメントとこの答えは良いノードです:http://stackoverflow.com/a/4812421/1063730 – yoshi

関連する問題