使得在引导这个code工作 [英] Making this code work on boot

查看:180
本文介绍了使得在引导这个code工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直停留在这几个星期,现在......我不能得到这个工作。我所试图做的是我的应用程序,当用户在插入耳机有/耳机插入手机承认,当他们这样做,因为它显示一个通知图标。这是我的课。我已经添加了

I have been stuck on this for weeks now...I can not get this to work. What I am trying to do is for my app to recognize when a user plugs in there earphones/headphones into the phone, and when they do, for it to show a notification icon. Here is my class. I have added the

 public class ItudeMobileBroadcastReceiver extends BroadcastReceiver {
     int NOTIFICATION_ID = 1234567890;
     NotificationManager mNotificationManager;

     @Override 
     public void onReceive(Context context, Intent intent) {
         /* Only perform this code if the BroadcastReceiver received the ACTION_BOOT_COMPLETED action.*/
         Log.d("Autobooted", "it booted");
         Toast laToast = Toast.makeText(context, "I have booted", 5);
         laToast.show();
         //It boots so it shows that toast. Rest doesn't work though    
         if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
             int state = intent.getIntExtra("state", -1);
             switch (state) {
             case 0:
                 Log.d("unplugged", "Headset was unplugged");
                 NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                 notificationManager.cancel(NOTIFICATION_ID);
                 Toast unpluggedToast = Toast.makeText(context, "Headset has been disconnected", Toast.LENGTH_SHORT);
                 unpluggedToast.show();
                 break;
             case 1:
                 Log.d("plugged", "Headset is plugged");
                 mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                 showNotification(context, R.drawable.notification_icon, "short", false);
                 Toast pluggedToast = Toast.makeText(context, "Headset has been connected", Toast.LENGTH_SHORT);
                 pluggedToast.show();
                 break;
             default:
                 Log.d("uh", "I have no idea what the headset state is");
                 Toast defaultToast = Toast.makeText(context, "No headset detected", Toast.LENGTH_SHORT);
                 defaultToast.show();
             }
         }
     }

     private void showNotification(Context context, int statusBarIconID, String string, boolean showIconOnly) {
         Intent contentIntent = new Intent();
         //Text that it says in the notification area when the notification appears
         String dropDownText = "Notification Title";
         Notification n = new Notification(R.drawable.notification_icon, "Notification Title", System.currentTimeMillis());
         n.flags = Notification.FLAG_ONGOING_EVENT;
         Intent intent = new Intent(context, MainActivity.class); //Activity that will be launched if notification is clicked
         PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
         n.setLatestEventInfo(context, "Notification Title", "Bottom Line", pending);
         mNotificationManager.notify(NOTIFICATION_ID, n);
     }
 }

这是我的清单

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

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

        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="18" />

        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <receiver android:name="com.example.testreciever.BootCompletedReceiver" >
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                </intent-filter>
            </receiver>
            <receiver 
                android:enabled="true"
                android:name=".ItudeMobileBroadcastReceiver"
                android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

    <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
    <intent-filter>
    <action android:name="android.intent.action.ACTION_HEADSET_PLUG" />
    </intent-filter>
    <intent-filter>
    <action android:name="android.intent.action.MODIFY_AUDIO_SETTINGS" />
    </intent-filter>

    </receiver>

            <activity
                android:name="com.example.testreciever.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name="com.example.testreciever.Hope"
                android:label="@string/title_activity_hope" >
            </activity>
        </application>

    </manifest>

在启动它并显示启动它吐司,所以我知道它的工作。但它没有显示通知图标,当我插上耳机。我真的不知道该怎么办了。如果你能帮助,请不要。太感谢你了。

On boot it does show the "it booted" toast, so I know it's working. But it's not showing the notification icon when I plug in the earphones. I really just don't know what to do anymore. If you can help, please do. Thank You so much.

我知道通知code ++工程,因为如果我把它放在一个活动课,我启动它的工作程序,但我不希望有启动应用程序。我只是希望它在引导工作。

I know the notification code works, because if i put it in an activity class and i launch the app it works, but I don't want to have to launch the app. I just want it to work on boot.

推荐答案

的<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/2.3.7_r1/com/android/server/HeadsetObserver.java#HeadsetObserver\"相对=nofollow> HeadsetObserver 类负责广播 Intent.ACTION_HEADSET_PLUG 时,在输入/输出插入耳机。下面是一些code从 HeadsetObserver.sendIntent()

The HeadsetObserver class is responsible for broadcasting Intent.ACTION_HEADSET_PLUG when a headset in plugged in/out. Here's some of the code from HeadsetObserver.sendIntent()

        //  Pack up the values and broadcast them to everyone
        Intent intent = new Intent(Intent.ACTION_HEADSET_PLUG);
        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
        int state = 0;
        int microphone = 0;

        if ((headset & HEADSETS_WITH_MIC) != 0) {
            microphone = 1;
        }
        if ((headsetState & headset) != 0) {
            state = 1;
        }
        intent.putExtra("state", state);
        intent.putExtra("name", headsetName);
        intent.putExtra("microphone", microphone);

        if (LOG) Slog.v(TAG, "Intent.ACTION_HEADSET_PLUG: state: "+state+" name: "+headsetName+" mic: "+microphone);
        // TODO: Should we require a permission?
        ActivityManagerNative.broadcastStickyIntent(intent, null);

正如你所看到的,其目的是标记有<一个href=\"http://developer.android.com/reference/android/content/Intent.html#FLAG_RECEIVER_REGISTERED_ONLY\"相对=nofollow> Intent.FLAG_RECEIVER_REGISTERED_ONLY 。这意味着,广播不能被接收的广播接收器中的Andr​​oidManifest.xml定义

As you can see, the intent is flagged with Intent.FLAG_RECEIVER_REGISTERED_ONLY. This means that the broadcast cannot be received by a BroadcastReceiver defined in your AndroidManifest.xml.

你必须有你的一些组件的应用程序运行,即一个服务活动,并注册 registerReceiver(广播接收器接收,IntentFilter的过滤器接收器)

You have to have some component of you app running, ie. a Service or an Activity, and register the receiver with registerReceiver(BroadcastReceiver receiver, IntentFilter filter)

修改,例如code

就像我说的,你需要有你的应用程序运行的一些组件,领取 Intent.ACTION_HEADSET_PLUG 播出。最好的选择可能是一个服务

As I said, you need to have some component of your app running, to receive the Intent.ACTION_HEADSET_PLUG broadcast. The best option is probably a Service.

所以让我们创建一个服务,HeadsetObserverService.java:

So lets create a Service, HeadsetObserverService.java:

public class HeadsetObserverService extends Service {

    private static final String TAG = "HeadsetObserverService";

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        // Create a filter. We are interested in the Intent.ACTION_HEADSET_PLUG action
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_HEADSET_PLUG);

        // Register a new HeadsetReceiver
        registerReceiver(new HeadsetReceiver(), filter);

        // We return START_STICKY. If our service gets destroyed, Android will try to restart it when resources are available.      
        return START_STICKY;
    }

    private class HeadsetReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
                int state = intent.getIntExtra("state", -1);
                switch (state) {
                case 0:
                     Log.d("unplugged", "Headset was unplugged");

                     // Cancel notification here

                break;
                case 1:
                     Log.d("plugged", "Headset is plugged");

                     // Show notification

                break;
                default:
                    Log.w("uh", "I have no idea what the headset state is");
                }
            }
        }
    }
}

如果你想要这个服务来自动启动,当手机重新启动,你需要一个广播接收器听为 Intent.ACTION_BOOT_COMPLETED 播出。

If you want this Service to be started automatically when the phone reboots, you'll need a BroadcastReceiver listening for the Intent.ACTION_BOOT_COMPLETED broadcast.

public class BootReceiver extends BroadcastReceiver {

    private static final String TAG = "BootReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "onReceive() - Received broadcast");

        if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            Log.i(TAG, "onReceive() - Intent action: " + Intent.ACTION_BOOT_COMPLETED);
            Log.i(TAG, "onReceive() - Starting service..");

            // Phone is booted, lets start the service
            Intent serviceIntent = new Intent(context, HeadsetObserverService.class);           
            context.startService(serviceIntent);
        }
    }
}

您需要在新的服务 BootReceiver 添加到您的Andr​​oidManifest

You need to add the new Service and the BootReceiver to your AndroidManifest

<receiver 
    android:name=".BootReceiver"
    android:enabled="true">
    <intent-filter> 
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

<service android:name=".HeadsetObserverService">

您也应该有一个活动,用户可以启动和停止 HeadsetObserverService

You should also have an Activity where the user can start and stop the HeadsetObserverService

这篇关于使得在引导这个code工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆