2016-04-22 16 views
0

我對android studio開發非常陌生,所以答案很簡單。 這就是說,我已經把這個問題弄糊塗了一天了:即使使用AlarmManager關閉了應用程序,我也試圖每10秒運行一段代碼。爲什麼我看不到由AlarmManager觸發的代碼段的日誌?

我複製粘貼什麼建議本指南中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 = 0×14 CMP = com.example.somelocation.myapplication/.MyAlarmReceiver

但我看不出有任何定位我的廣播接收器或我的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> 

我錯過了什麼?

更新: 明白了!這是我的清單文件中的一個錯誤: 主要活動的close標籤在接收器聲明之後。應該是:

<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

被打印在控制檯上的日誌...這裏是我的廣播接收器和獲取

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 

您正在創建一個單獨的進程(「遠程」)日誌您的logcat可能會被設置爲首先顯示主進程創建。

見截圖 - ​​ enter image description here

所以申請過程中,從下拉列表中更改,還可以選擇日誌級別設置爲詳細。

+0

我禁用了所有的過濾器,我仍然無法看到它。想法? –

相關問題