receiver で一部のアクションが受信できないので悩んでいます

954 views
Skip to first unread message

ta-ka

unread,
Aug 1, 2015, 9:46:58 PM8/1/15
to Android-SDK-Japan
はじめまして。

BroadcastReceiver で、 SHUTDOWN や WIFI_STATE_CHANGED は受信できるのですが、 DATE_CHANGED や 
BOOT_COMPLETED が受け取れなくて悩んでいます。
Android Studio のテンプレートから作ってみましたが、何が問題で動かないのかわかりませんでした。

エミュレータ、実機(Galaxy S5) 共にうまく受信できません。
BOOT_COMPLETED は、logcat を見ると、他のアプリは受信できているような感じでしたが、私のパッケージでは受信できていないようでした。

何かお知恵を拝借できれば幸いです。

よろしくお願いいたします。


バージョン等は以下で行っています。

Android studio 1.2.2


compileSdkVersion 22
buildToolsVersion "23.0.0 rc3"


minSdkVersion 15
targetSdkVersion 22


以下、ファイル内容です。

AndroidManifest.xml
--------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest ・・・ >
<application ・・・ >
<activity

</activity>

<receiver
android:name=".MyReceiver"
android:enabled="true"
>
<intent-filter>
<action android:name="android.intent.action.REBOOT" />
<action android:name="android.intent.action.DATE_CHANGED" />
<action android:name="android.intent.action.SHUTDOWN" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
</manifest>
--------------------------------------------



MyReceiver.java
--------------------------------------------
public class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive( Context context, Intent intent ){
SharedPreferences pref = context.getApplicationContext().getSharedPreferences( "test", Context.MODE_MULTI_PROCESS );
SharedPreferences.Editor edit = pref.edit();

edit.putString( "test", intent.getAction() + "\n" + pref.getString( "test", "" ) );
edit.commit();

Log.d( "test", intent.getAction() );
}
}
--------------------------------------------


MyActivity.java
--------------------------------------------
public class MainActivity extends ActionBarActivity{

@Override
protected void onCreate( Bundle savedInstanceState ){
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );

SharedPreferences pref = getApplicationContext().getSharedPreferences( "test", Context.MODE_MULTI_PROCESS );
((TextView)findViewById( R.id.hello )).setText( pref.getString( "test", "No data" ) );
}
:
}
--------------------------------------------

Hirokazu Fukami

unread,
Aug 1, 2015, 10:04:46 PM8/1/15
to Android-SDK-Japan
こんにちはfkmです。

AndroidManifestを次のように書き換えてみた場合はどうですか?

<receiver
    android:name=".MyReceiver"
    android:enabled="true">
    <intent-filter>
        <action android:name="android.intent.action.REBOOT" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.DATE_CHANGED" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.SHUTDOWN" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>
--
このメールは Google グループのグループ「Android-SDK-Japan」に登録しているユーザーに送られています。
このグループから退会し、グループからのメールの配信を停止するには android-sdk-ja...@googlegroups.com にメールを送信してください。
このグループに投稿するには android-...@googlegroups.com にメールを送信してください。
http://groups.google.com/group/android-sdk-japan からこのグループにアクセスしてください。
その他のオプションについては https://groups.google.com/d/optout にアクセスしてください。
Reply all
Reply to author
Forward
0 new messages