###やりたいこと
サービスを使用して、バックグラウンドでもタイマーを実行し続けたい。
エラー内容
サービスを実行するボタンをクリックしても実行されない。
ボタンのクリックには反応している。
###コード
MainActivity
Java
1package teityan.com.myapplication; 2 3import android.app.Service; 4import android.content.Intent; 5import android.support.v7.app.AppCompatActivity; 6import android.os.Bundle; 7import android.util.Log; 8 9public class MainActivity extends AppCompatActivity { 10 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.activity_main); 15 // Serviceを呼び出す 16 Intent intent = new Intent(getApplication(), ServiceTimer.class); 17 startService(intent); 18 Log.d("Service", "received"); 19 } 20} 21
ServiceTimer
Java
1import android.app.Service; 2import android.content.Intent; 3import android.os.IBinder; 4import android.util.Log; 5import android.widget.Toast; 6 7import java.util.Timer; 8import java.util.TimerTask; 9 10/** 11 * Created by tenko_w8othx0 on 2017/08/29. 12 */ 13 14public class ServiceTimer extends Service{ 15 16 private Timer timer = null; 17 private int count = 0; 18 19 @Override 20 public void onCreate() { 21 super.onCreate(); 22 } 23 24 @Override 25 public int onStartCommand(Intent intent, int flags, int startId) { 26 Log.d("service", "onStartCommand"); 27 28 timer = new Timer(); 29 timer.schedule( new TimerTask(){ 30 @Override 31 public void run(){ 32 Log.d( "TestService" , "count = "+ count ); 33 count++; 34 Toast.makeText(getApplicationContext(), "ALARM 1", Toast.LENGTH_SHORT).show(); 35 } 36 }, 0, 1000); 37 38 return super.onStartCommand(intent, flags, startId); 39 } 40 41 @Override 42 public void onDestroy() { 43 Log.d("service", "onDestroy"); 44 super.onDestroy(); 45 // timer cancel 46 if( timer != null ){ 47 timer.cancel(); 48 timer = null; 49 } 50 51 } 52 53 @Override 54 public IBinder onBind(Intent intent) { 55 // エラーになるので、とりあえず入れてありますが使いません 56 return null; 57 } 58 59 60} 61
Mainfest
XML
1<?xml version="1.0" encoding="utf-8"?> 2<manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="パッケージ"> 4 5 <uses-permission android:name="android.permission.INTERNET" /> 6 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 7 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 8 <uses-permission android:name="android.permission.WAKE_LOCK" /> 9 10 <service android:name=".ServiceTimer" /> 11 12 <receiver 13 android:name=".AlarmReceiver" 14 android:process=":remote" /> 15 16 <application 17 android:allowBackup="true" 18 android:icon="@mipmap/ic_launcher" 19 android:label="@string/app_name" 20 android:supportsRtl="true" 21 android:theme="@style/AppTheme"> 22 <activity 23 android:name=".MainActivity" 24 android:label="@string/app_name" 25 android:theme="@style/AppTheme.NoActionBar"> 26 <intent-filter> 27 <action android:name="android.intent.action.MAIN" /> 28 29 <category android:name="android.intent.category.LAUNCHER" /> 30 </intent-filter> 31 </activity> 32 <activity android:name=".WebViewActivity" /> 33 <activity android:name=".InfoActivity" /> 34 <activity android:name=".ServiceActivity" /> 35 </application> 36 37</manifest>
ログです。
Log
1I/OpenGLRenderer: Initialized EGL, version 1.4 2I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@553f725 time:19481794 3W/art: Suspending all threads took: 22.859ms 4W/art: Verification of void android.support.v4.view.AbsSavedState.<clinit>() took 209.698ms 5I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@553f725 time:19490698 6I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@553f725 time:19490698 7I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@553f725 time:19525176 8 908/29 23:15:14: Launching app 10W/System: ClassLoader referenced unknown path: パス 11Hot swapped changes, activity restarted 12D/Service: received 13V/RenderScript: 0x55b5e9a010 Launching thread(s), CPUs 8 14D/Service: received
###備考
AndroidのバージョンはAndroid6.0です。
よろしくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。