私は非常にアンドロイドスタジオの開発に新しいので、答えはおそらく簡単です。言われていること 、私は今日の問題のまわりで私の頭を逮捕しました:/AlarmManagerによってトリガされたコードのログが表示されないのはなぜですか?
私はアプリがAlarmManagerを使ってクローズされた場合でも、10秒ごとにコードの塊を実行しようとしています。
私はコピー&ペーストhttps://guides.codepath.com/android/Starting-Background-Services
このガイドで提案されたものを、私は結果によって少し困惑してる - 私がログに示されているように、私はアラームが、10秒ごとにトリガーされますかと思う:
04-20 21:25:44.125 557-632 /? D/AlarmManager:トリガーアラーム22ad4130 RTC_WAKEUP IntentSender {220fb650:PendingIntentRecord {2243b570 com.example.somelocation.myapplication broadcastIntent}} 04-20 21:25:44.125 557-557 /? V/AlarmManager:FLG = 0x14の CMP = com.example.somelocation.myapplication/.MyAlarmReceiver
をしかし、私は私のBroadcastReceiverや私IntentServiceの内側に位置する私のカスタムログのいずれかを見ることができない:トリガー。
MyTestService:
package com.example.somelocation.myapplication;
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
public class MyTestService extends IntentService {
public MyTestService() {
super("MyTestService");
}
@Override
protected void onHandleIntent(Intent intent) {
// Do the task here
Log.i("MyTestService", "Service running"); // Can't see this log
}
}
MyAlarmReceiver:
package com.example.somelocation.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyAlarmReceiver extends BroadcastReceiver {
public static final int REQUEST_CODE = 12345;
@Override
public void onReceive(Context context, Intent intent) {
Log.e("MyTestService","broadcastreciever onReceive called"); // Can't see this log
Intent i = new Intent(context, MyTestService.class);
i.putExtra("foo", "bar");
context.startService(i);
}
}
主な活動:
package com.example.somelocation.myapplication;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView batteryInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scheduleAlarm();
}
public void scheduleAlarm() {
Intent intent = new Intent(getApplicationContext(), MyAlarmReceiver.class);
final PendingIntent pIntent = PendingIntent.getBroadcast(this, MyAlarmReceiver.REQUEST_CODE,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
long firstMillis = System.currentTimeMillis();
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis,
10000, pIntent);
}
}
私のマニフェスト:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.somelocation.myapplication">
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<receiver
android:name=".MyAlarmReceiver"
android:process=":remote" >
</receiver>
<service
android:name=".MyTestService"
android:exported="false"/>
</activity>
</application>
</manifest>
私は何が欠けていますか?
更新: はそれを手に入れました!マニフェストファイルにエラーがありました: メインアクティビティの終了タグが受信者宣言の後にありました。次のようになります。
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MyAlarmReceiver"
android:process=":remote" >
</receiver>
<receiver
android:name=".BootReceiver"
android:enabled="true"
android:exported="true"/>
</application>
すべてのフィルタを無効にしましたが、まだ表示されません。思考? –