2013-05-15 8 views
1

안드로이드를 처음 사용했습니다. 장치에서 배경 화면이 변경되면 알림 표시 줄에 메시지를 보내는 브로드 캐스트 수신기를 사용하는 응용 프로그램을 만들려고합니다. 기기에 성공적으로 설치되었지만 예상대로 작동하지 않습니다. 여기에 코드브로드 캐스트 수신기

WallPagerNotificationReceiver.java

import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.net.Uri; 

import android.util.Log; 
import android.widget.Toast; 

public class WallPaperNotificationReceiver extends BroadcastReceiver { 

       @Override 
       public void onReceive(Context context, Intent intent) { 
           this.sendNotification(context, "You have changed Wallpaper"); 
    } 
    private void sendNotification(Context ctx, String message) 
    { 
       //Get the notification manager 
       String ns = Context.NOTIFICATION_SERVICE; 
       NotificationManager nm = 
           (NotificationManager)ctx.getSystemService(ns); 

       //Create Notification Object 
           int icon = R.drawable.ic_launcher; 
           CharSequence tickerText = "Hello"; 
           long when = System.currentTimeMillis(); 

           Notification notification = 
               new Notification(icon, tickerText, when); 

           //Set ContentView using setLatestEvenInfo 
        Intent intent = new Intent(Intent.ACTION_VIEW); 
        intent.setData(Uri.parse("http://www.google.com")); 
        PendingIntent pi = PendingIntent.getActivity(ctx, 0, intent, 0); 
        notification.setLatestEventInfo(ctx, "Intimation", message, pi); 

        //Send notification 
           nm.notify(1, notification); 
           Toast.makeText(ctx,"Hello Nawin",Toast.LENGTH_LONG).show(); 
    } 

} 

Manifest.xlm

<xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="in.ac.srmuniv" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="15" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 

    <receiver android:name=".WallPaperNotificationReceiver"> 
      <intent-filter> 
      <action android:name="android.intent.action.WALLPAPER_CHANGED" /> 
      </intent-filter> 
    </receiver> 
</application> 
</manifest> 

은 방송 수신기를 사용하려면이 올바른 방법은 무엇입니까? 그렇다면 내가 실수 한 부분을 도와 주시겠습니까?

미리 감사드립니다.

P .: 활동 또는 서비스를 사용하지 않습니다.

<receiver android:name=".WallPaperNotificationReceiver"> 
     <intent-filter> 
     <action android:name="android.intent.action.ACTION_WALLPAPER_CHANGED" /> 
     </intent-filter> 
</receiver> 

이 실제 방송입니다 :

http://developer.android.com/reference/android/content/Intent.html#ACTION_WALLPAPER_CHANGED

+0

질문이 명확하지 않습니다. 예상되는 행동은 무엇이며 대신 무엇을보고 있습니까? – Kuffs

+0

내 예상 결과는 내가 배경 화면을 변경할 때 알림 메시지를 얻는 것입니다. 내 문제는 지금 나는 어떤 알림도 내가 벽지를 변경하지 않고있어. – Vijay

+0

나는 수신기에 대한 수정을 넣어. 그러나 알림 코드는 테스트하지 않았습니다. onReceive 메소드에서 토스트를 사용하여 방송을 수신했는지 확인하는 것이 가장 좋습니다. –

답변

3

Android 버전 3.1 BroadcastReceivers는 매니페스트에만 등록되어 있으며 활동이없는 앱에서는 작동하지 않으므로 작동하지 않습니다. 각 앱에는 수신자가 작동하도록 적어도 한 번 실행해야하는 활동이 있어야합니다. 이것은 악성 코드를 막기위한 것입니다. 당신은 아무것도하지 않는 더미 활동을 한 번 실행하고 수신기를 작동하게해야합니다.

1

변경 수신기를 개최하여 전경 처리를 할 수 있습니다 브로드 캐스트 수신기가 전혀 호출되지 않았는지 확인하기 위해 코드에 문을 로깅합니다.

그렇지 않을 경우, 이에 대한 원인 (잘못된 매니페스트 등)을 조사

방송 수신기 알림이 잘못 빌드 코드를 호출 해하는 경우.

+0

내가 언급 한대로 수신자가 바뀌어도 작동하지 않는다. – Vijay

+0

NickT의 게시물을 참조하십시오. 내가 알지 못했지만 그런 일을 할 필요가 없었습니다. 그러나 위의 수신기는 정확합니다. –

0

일부를 넣어 프로세스 수명주기에 따라 우리는 방송 수신기 http://developer.android.com/guide/components/processes-and-threads.html#Threads

+0

logging statement를 사용하여 브로드 캐스트 수신기가 트리거되지 않음을 확인했습니다. ( – Vijay

+0

트리거되지 않은 경우 매니페스트에 문제가있는 경우 ".WallPaperNotificatioBReceiver"대신 전체 패키지 이름을 지정하고 수신자의 이름이 올바른지 확인하십시오 당신이 당신의 매니 페스트에서 선언하고있는 것과 정확히 동일합니다. – Kuffs

관련 문제