Ich bin sehr neu in Android Studio-Entwicklung, so dass die Antwort wahrscheinlich trivial ist. Das gesagt, ich habe meinen Kopf um das Problem für einen Tag jetzt:/Warum kann ich keine Logs von Codeteilen sehen, die von AlarmManager ausgelöst wurden?
Ich versuche, ein Stück Code alle 10 Sekunden, auch wenn die App mit AlarmManager geschlossen ist.
Ich kopiere kleistert, was in diesem Leitfaden vorgeschlagen wird https://guides.codepath.com/android/Starting-Background-Services
ich durch das Ergebnis ein wenig verwirrt bin - Ich glaube, ich bekomme Alarm alle 10 Sekunden ausgelöst, wie im Protokoll angegeben:
04-20 21: 25: 44.125 557-632 /? D/AlarmManager: Ausgelöster Alarm 22ad4130 RTC_WAKEUP IntentSender {220fb650: PendingIntentRecord {2243b570 com.beispiel.somelocation.myapplication broadcastIntent}} 04-20 21: 25: 44.125 557-557 /? V/Alarmmanager: ausgelöst: flg = 0x14 cmp = com.example.somelocation.myapplication/.MyAlarmReceiver
Aber ich kann keine meiner benutzerdefinierten Protokolle positioniert in meinem BroadcastReceiver oder meine IntentService sehen.
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);
}
}
Hauptaktivität:
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);
}
}
mein Manifest:
<?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>
Was fehlt mir?
Update: Verstanden! Es war ein Fehler in meiner Manifest-Datei: Das Close-Tag der Hauptaktivität war nach der Empfängererklärung. sein sollte:
<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>
Ich habe alle Filter deaktiviert und kann es immer noch nicht sehen. Gedanken? –