2016-04-22 6 views
0

私は非常にアンドロイドスタジオの開発に新しいので、答えはおそらく簡単です。言われていること 、私は今日の問題のまわりで私の頭を逮捕しました:/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> 

答えて

0

ログがコンソールに印刷されている...ここで私はあなたがBroadcastReceiver用(「リモート」)別のプロセスを作成している

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

すべてのフィルタを無効にしましたが、まだ表示されません。思考? –

関連する問題