Ekeon

Android Service Restart

PersistentService.java

public class PersistentService extends Service {

<span class="nd">@Override</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="n">onCreate</span><span class="o">()</span> <span class="o">{</span>
        <span class="n">Log</span><span class="o">.</span><span class="na">d</span><span class="o">(</span><span class="s">"PersistentService"</span><span class="o">,</span> <span class="s">"onCreate"</span><span class="o">);</span>
        <span class="n">unregisterRestartAlarm</span><span class="o">();</span> <span class="c1">// 만들어질때 알람 해제해줌.</span>
        <span class="kd">super</span><span class="o">.</span><span class="na">onCreate</span><span class="o">();</span>
<span class="o">}</span>

<span class="nd">@Override</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="n">onDestroy</span><span class="o">()</span> <span class="o">{</span>
        <span class="n">Log</span><span class="o">.</span><span class="na">d</span><span class="o">(</span><span class="s">"PersistentService"</span><span class="o">,</span><span class="s">"onDestroy"</span><span class="o">);</span>

        <span class="n">registerRestartAlarm</span><span class="o">();</span>  <span class="c1">// 서비스 종료될때 알람등록해줌.</span>
        <span class="kd">super</span><span class="o">.</span><span class="na">onDestroy</span><span class="o">();</span>
<span class="o">}</span>

<span class="kd">public</span> <span class="kt">void</span> <span class="n">registerRestartAlarm</span><span class="o">()</span> <span class="o">{</span>
        <span class="n">Intent</span> <span class="n">intent</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Intent</span><span class="o">(</span><span class="n">PersistentService</span><span class="o">.</span><span class="na">this</span><span class="o">,</span> <span class="n">RestartService</span><span class="o">.</span><span class="na">class</span><span class="o">);</span>
        <span class="n">intent</span><span class="o">.</span><span class="na">setAction</span><span class="o">(</span><span class="s">"ACTION.RESTART.PersistentService"</span><span class="o">);</span>

        <span class="n">PendingIntent</span> <span class="n">sender</span> <span class="o">=</span> <span class="n">PendingIntent</span><span class="o">.</span><span class="na">getBroadcast</span><span class="o">(</span><span class="n">PersistentService</span><span class="o">.</span><span class="na">this</span><span class="o">,</span> <span class="mi">0</span><span class="o">,</span> <span class="n">intent</span><span class="o">,</span> <span class="mi">0</span><span class="o">);</span>

        <span class="kt">long</span> <span class="n">firstTime</span> <span class="o">=</span> <span class="n">SystemClock</span><span class="o">.</span><span class="na">elapsedRealtime</span><span class="o">();</span>
        <span class="n">firstTime</span> <span class="o">+=</span> <span class="mi">1000</span> <span class="o">*</span> <span class="mi">10</span><span class="o">;</span>

        <span class="n">AlarmManager</span> <span class="n">am</span> <span class="o">=</span> <span class="o">(</span><span class="n">AlarmManager</span><span class="o">)</span><span class="n">getSystemService</span><span class="o">(</span><span class="n">ALARM_SERVICE</span><span class="o">);</span>
        <span class="n">am</span><span class="o">.</span><span class="na">setRepeating</span><span class="o">(</span><span class="n">AlarmManager</span><span class="o">.</span><span class="na">ELAPSED_REALTIME_WAKEUP</span><span class="o">,</span> <span class="n">firstTime</span> <span class="o">*</span> <span class="mi">10</span><span class="o">,</span> <span class="mi">1000</span><span class="o">,</span> <span class="n">sender</span><span class="o">);</span>
<span class="o">}</span>

<span class="kd">public</span> <span class="kt">void</span> <span class="n">unregisterRestartAlarm</span><span class="o">()</span> <span class="o">{</span>
        <span class="n">Intent</span> <span class="n">intent</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Intent</span><span class="o">(</span><span class="n">PersistentService</span><span class="o">.</span><span class="na">this</span><span class="o">,</span> <span class="n">RestartService</span><span class="o">.</span><span class="na">class</span><span class="o">);</span>
        <span class="n">intent</span><span class="o">.</span><span class="na">setAction</span><span class="o">(</span><span class="s">"ACTION.RESTART.PersistentService"</span><span class="o">);</span>

        <span class="n">PendingIntent</span> <span class="n">sender</span> <span class="o">=</span> <span class="n">PendingIntent</span><span class="o">.</span><span class="na">getBroadcast</span><span class="o">(</span><span class="n">PersistentService</span><span class="o">.</span><span class="na">this</span><span class="o">,</span> <span class="mi">0</span><span class="o">,</span> <span class="n">intent</span><span class="o">,</span> <span class="mi">0</span><span class="o">);</span>

        <span class="n">AlarmManager</span> <span class="n">am</span> <span class="o">=</span> <span class="o">(</span><span class="n">AlarmManager</span><span class="o">)</span><span class="n">getSystemService</span><span class="o">(</span><span class="n">ALARM_SERVICE</span><span class="o">);</span>
        <span class="n">am</span><span class="o">.</span><span class="na">cancel</span><span class="o">(</span><span class="n">sender</span><span class="o">);</span>
<span class="o">}</span>

<span class="nd">@Override</span>
<span class="kd">public</span> <span class="n">IBinder</span> <span class="n">onBind</span><span class="o">(</span><span class="n">Intent</span> <span class="n">intent</span><span class="o">)</span> <span class="o">{</span>
        <span class="n">Log</span><span class="o">.</span><span class="na">d</span><span class="o">(</span><span class="s">"TAG"</span><span class="o">,</span> <span class="s">"RESTART SERVICE"</span><span class="o">);</span> <span class="c1">// Service 를 extend 받을시 필수</span>
        <span class="k">return</span> <span class="kc">null</span><span class="o">;</span>
<span class="o">}</span>

}

RestartService.java

public class RestartService extends BroadcastReceiver{

<span class="nd">@Override</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="n">onReceive</span><span class="o">(</span><span class="n">Context</span> <span class="n">context</span><span class="o">,</span> <span class="n">Intent</span> <span class="n">intent</span><span class="o">)</span> <span class="o">{</span>
    <span class="n">Log</span><span class="o">.</span><span class="na">d</span><span class="o">(</span><span class="s">"RestartService"</span><span class="o">,</span> <span class="s">"RestartService called! :"</span> <span class="o">+</span> <span class="n">intent</span><span class="o">.</span><span class="na">getAction</span><span class="o">());</span>

    <span class="k">if</span><span class="o">(</span><span class="n">intent</span><span class="o">.</span><span class="na">getAction</span><span class="o">().</span><span class="na">equals</span><span class="o">(</span><span class="s">"ACTION.RESTART.PERSISTENTSERVICE"</span><span class="o">)){</span>
        <span class="n">Log</span><span class="o">.</span><span class="na">d</span><span class="o">(</span><span class="s">"RestartService"</span><span class="o">,</span> <span class="s">"ACTION_RESTART_PERSISTENTSERVICE"</span><span class="o">);</span>
        <span class="n">Intent</span> <span class="n">i</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Intent</span><span class="o">(</span><span class="n">context</span><span class="o">,</span> <span class="n">PersistentService</span><span class="o">.</span><span class="na">class</span><span class="o">);</span>
        <span class="n">context</span><span class="o">.</span><span class="na">startService</span><span class="o">(</span><span class="n">i</span><span class="o">);</span>
    <span class="o">}</span>

    <span class="k">if</span> <span class="o">(</span><span class="n">intent</span><span class="o">.</span><span class="na">getAction</span><span class="o">().</span><span class="na">equals</span><span class="o">(</span><span class="n">Intent</span><span class="o">.</span><span class="na">ACTION_BOOT_COMPLETED</span><span class="o">))</span> <span class="o">{</span>
        <span class="n">Log</span><span class="o">.</span><span class="na">d</span><span class="o">(</span><span class="s">"RestartService"</span><span class="o">,</span> <span class="s">"ACTION_BOOT_COMPLETED"</span><span class="o">);</span>
        <span class="n">Intent</span> <span class="n">i</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Intent</span><span class="o">(</span><span class="n">context</span><span class="o">,</span> <span class="n">PersistentService</span><span class="o">.</span><span class="na">class</span><span class="o">);</span>
        <span class="n">context</span><span class="o">.</span><span class="na">startService</span><span class="o">(</span><span class="n">i</span><span class="o">);</span>
    <span class="o">}</span>
<span class="o">}</span>

}

MainActivity.java

BroadcastReceiver receiver = new RestartService();

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

<span class="n">Intent</span> <span class="n">intentMyService</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Intent</span><span class="o">(</span><span class="k">this</span><span class="o">,</span> <span class="n">PersistentService</span><span class="o">.</span><span class="na">class</span><span class="o">);</span>
<span class="n">receiver</span> <span class="o">=</span> <span class="k">new</span> <span class="n">RestartService</span><span class="o">();</span>

<span class="k">try</span><span class="o">{</span>
    <span class="n">IntentFilter</span> <span class="n">mainFilter</span> <span class="o">=</span> <span class="k">new</span> <span class="n">IntentFilter</span><span class="o">(</span><span class="s">"com.ekeon.clipboardtest.restart"</span><span class="o">);</span>
    <span class="n">registerReceiver</span><span class="o">(</span><span class="n">receiver</span><span class="o">,</span> <span class="n">mainFilter</span><span class="o">);</span>
    <span class="n">startService</span><span class="o">(</span><span class="n">intentMyService</span><span class="o">);</span>
<span class="o">}</span>
<span class="k">catch</span> <span class="o">(</span><span class="n">Exception</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
    <span class="n">Log</span><span class="o">.</span><span class="na">d</span><span class="o">(</span><span class="s">"RestartService"</span><span class="o">,</span><span class="n">e</span><span class="o">.</span><span class="na">getMessage</span><span class="o">()+</span><span class="s">""</span><span class="o">);</span>
<span class="o">}</span>

}

@Override protected void onDestroy() { unregisterReceiver(receiver); super.onDestroy(); }

### AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest package="com.ekeon.clipboardtest" xmlns:android="http://schemas.android.com/apk/res/android">

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

<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">

<span class="nt">&lt;activity</span> <span class="na">android:name=</span><span class="s">".MainActivity"</span><span class="nt">&gt;</span>
  <span class="nt">&lt;intent-filter&gt;</span>
    <span class="nt">&lt;action</span> <span class="na">android:name=</span><span class="s">"android.intent.action.MAIN"</span><span class="nt">/&gt;</span>
    <span class="nt">&lt;category</span> <span class="na">android:name=</span><span class="s">"android.intent.category.LAUNCHER"</span><span class="nt">/&gt;</span>
  <span class="nt">&lt;/intent-filter&gt;</span>
<span class="nt">&lt;/activity&gt;</span>

<span class="nt">&lt;service</span>
    <span class="na">android:name=</span><span class="s">".PersistentService"</span>
    <span class="na">android:enabled=</span><span class="s">"true"</span>
    <span class="na">android:process=</span><span class="s">":remote"</span><span class="nt">&gt;</span>
  <span class="nt">&lt;intent-filter&gt;</span>
    <span class="nt">&lt;action</span> <span class="na">android:name=</span><span class="s">"com.ekeon.clipboardtest.restart"</span><span class="nt">/&gt;</span>
  <span class="nt">&lt;/intent-filter&gt;</span>
<span class="nt">&lt;/service&gt;</span>

<span class="nt">&lt;receiver</span>
    <span class="na">android:name=</span><span class="s">".RestartService"</span>
    <span class="na">android:enabled=</span><span class="s">"true"</span>
    <span class="na">android:exported=</span><span class="s">"false"</span>
    <span class="na">android:label=</span><span class="s">"RestartService"</span>
    <span class="na">android:process=</span><span class="s">":remote"</span><span class="nt">&gt;</span>
  <span class="nt">&lt;intent-filter&gt;</span>
    <span class="nt">&lt;action</span> <span class="na">android:name=</span><span class="s">"ACTION.RESTART.PersistentService"</span><span class="nt">/&gt;</span>
    <span class="nt">&lt;action</span> <span class="na">android:name=</span><span class="s">"android.intent.action.BOOT_COMPLETED"</span><span class="nt">/&gt;</span>
  <span class="nt">&lt;/intent-filter&gt;</span>

<span class="nt">&lt;/receiver&gt;</span>

</application>

</manifest>