无法捕获android中的自定义意图?
我有一个活动和一个按钮,使快捷方式+自定义意图与我的行动内。当我点击按钮后,它工作正常,一个快捷方式已经出现在主屏幕上。然后我点击快捷键,主活动无法捕捉我的receiver类的意图。我错在哪里 主要活动 我在onCreate中有rigister接收器,带有意向过滤器无法捕获android中的自定义意图?,android,android-intent,android-activity,broadcastreceiver,Android,Android Intent,Android Activity,Broadcastreceiver,我有一个活动和一个按钮,使快捷方式+自定义意图与我的行动内。当我点击按钮后,它工作正常,一个快捷方式已经出现在主屏幕上。然后我点击快捷键,主活动无法捕捉我的receiver类的意图。我错在哪里 主要活动 我在onCreate中有rigister接收器,带有意向过滤器 registerReceiver(new MyReceiver(), new IntentFilter("nhq.intent.flashlight")); 这是我在MainActivity内的Receiver类 public c
registerReceiver(new MyReceiver(), new IntentFilter("nhq.intent.flashlight"));
这是我在MainActivity内的Receiver类
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();
}
我还在清单文件中定义了Receiver
<?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>
我重新阅读了问题抱歉,您的包裹名称似乎有一个输入错误:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="nhq.fashlight"
shortcutIntent.setAction("nhq.intent.flashlight");
这可能就是快捷方式不起作用的原因。我认为需要添加操作以实现操作屏幕的关闭、打开和显示 像这样
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
同时添加操作屏幕,操作用户显示过滤器。添加操作。
然后接受意图行动
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Log.v("$$$$$$", "In Method: ACTION_SCREEN_OFF");
// onPause() will be called.
}
你能为我解释一下为什么当我有一个nhq.flashlight类型时,快捷方式不起作用吗???@QuocNguyen你的包nhq.flashlight现在不时尚了,它仍然不起作用吗?