广播接收器

时间:2013-05-15 13:11:51

标签: android broadcastreceiver

我是Android的新手,我正在尝试使用广播接收器执行应用程序,当设备上的壁纸更改时,它会将消息发送到通知栏。它已成功安装在设备上但未按预期工作。这是代码

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.S:我没有使用任何活动或服务。根据流程生命周期,我们可以通过托管广播接收器http://developer.android.com/guide/components/processes-and-threads.html#Threads

来实现前台流程

3 个答案:

答案 0 :(得分:4)

由于仅在清单和没有活动的应用程序中注册的Android 3.1版BroadcastReceivers将无效。每个应用必须具有必须至少运行一次的活动才能使接收器工作。这是为了防止恶意软件。你只需要一个不做任何事情的虚拟活动,然后退出运行一次,让你的接收器工作。

答案 1 :(得分:2)

将接收器更改为:

<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

答案 2 :(得分:1)

在代码中放入一些日志记录语句,以确定是否正在调用广播接收器。

如果不是,请调查原因(不正确的清单等)

如果正在调用广播接收器,那么构建通知的代码是错误的。

相关问题