【问题标题】:Can not catch the custom intent in android?无法在 android 中捕获自定义意图?
【发布时间】:2014-08-29 08:32:49
【问题描述】:

我有一个活动,一个按钮在我的内部操作中创建快捷方式 + 自定义意图。单击按钮后,它运行良好,主屏幕上已经有一个快捷方式。然后我点击快捷方式,主要活动无法在我的接收器类中捕捉到意图。我哪里错了?

主要活动

我在 onCreate() 中有 rigister Reciever,带有意图过滤器。

registerReceiver(new MyReceiver(), new IntentFilter("nhq.intent.flashlight"));

这是我在 MainActivity 中的 Reciever 类

public class MyReceiver extends BroadcastReceiver {

        @Override

        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                Log.v("$$$$$$", "In Method:  ACTION_SCREEN_OFF");
                // onPause() will be called.
            } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                Log.v("$$$$$$", "In Method:  ACTION_SCREEN_ON");

            } else if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
                Log.v("$$$$$$", "In Method:  ACTION_USER_PRESENT");
                // Handle resuming events
            }
        }

    }

当我点击按钮制作快捷方式时,我运行该功能

private void makeShortcut() {
        Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
        shortcutIntent.setAction("nhq.intent.flashlight"); 

        Intent addIntent = new Intent();
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,"Flash Light");
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(),R.drawable.ic_launcher));

        addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        getApplicationContext().sendBroadcast(addIntent); 

        //finish();
    }

我还在清单文件中定义了接收者。

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
    <!-- Camera Requirement -->
    <uses-feature android:name="android.hardware.camera" />

    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
    <!-- Camera Permissions -->
    <uses-permission android:name="android.permission.CAMERA" />

    <!-- Features -->
    <uses-feature android:name="android.hardware.camera.flash" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="nhq.fashlight.MainActivity"
            android:label="@string/app_name"
            android:launchMode="singleTask" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.CREATE_SHORTCUT" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <receiver android:name="nhq.fashlight.MainActivity$MyReceiver" >
            <intent-filter>
                <action android:name="nhq.intent.flashlight" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

【问题讨论】:

    标签: android android-intent android-activity broadcastreceiver


    【解决方案1】:

    我重新阅读了这个问题(抱歉),您的包名似乎有错字:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="nhq.fashlight"
    
    shortcutIntent.setAction("nhq.intent.flashlight"); 
    

    这可能是快捷方式不起作用的原因。

    【讨论】:

    • 您能解释一下为什么当我输入“nhq.flashlight”时,快捷方式不起作用吗??
    • @QuocNguyen 那么你的包 nhq.flashlight 现在不是 fashlight 并且它仍然不起作用?
    【解决方案2】:

    我认为 ACTION_SCREEN_OFF、ON、PRESENT 需要 addAction。

    喜欢这个

    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    

    同时添加 ACTION_SCREEN_ON,ACTION_USER_PRESENT filter.addAction。 然后接收意图操作

    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                    Log.v("$$$$$$", "In Method:  ACTION_SCREEN_OFF");
                    // onPause() will be called.
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-22
      • 1970-01-01
      • 1970-01-01
      • 2011-09-22
      • 2013-03-01
      • 2023-03-30
      相关资源
      最近更新 更多