我对android studio开发很新,所以答案可能很简单。 话虽这么说,我现在已经把问题困扰了一天:/
即使使用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>
答案 0 :(得分:0)
日志正在控制台上打印......这是我正在获取的日志
04-22 18:30:28.421 1830-4146/com.example.somelocation.myapplication I/MyTestService: Service running
04-22 18:30:45.337 2641-2641/com.example.somelocation.myapplication:remote E/MyTestService: broadcastreciever onReceive called
04-22 18:30:45.344 1830-4394/com.example.somelocation.myapplication I/MyTestService: Service running
04-22 18:30:48.4032641-2641/com.example.somelocation.myapplication:remote E/MyTestService: broadcastreciever onReceive called
您正在为BroadcastReceiver创建一个单独的进程('remote'),您的logcat可能会被设置为首先仅显示主进程。
因此,从下拉列表中更改应用程序进程,并选择日志级别为Verbose。