我正在尝试制作一个应用程序来检测用户何时拍照。我设置了一个广播接收器类并通过以下方式将其注册到清单文件中:
<receiver android:name="photoReceiver" >
<intent-filter>
<action android:name="com.android.camera.NEW_PICTURE"/>
<data android:mimeType="image/*"/>
</intent-filter>
</receiver>
无论我尝试做什么,程序都不会收到广播。这是我的接收器类:
public class photoReceiver extends BroadcastReceiver {
private static final String TAG = "photoReceiver";
@Override
public void onReceive(Context context, Intent intent) {
CharSequence text = "caught it";
int duration = Toast.LENGTH_LONG;
Log.d(TAG, "Received new photo");
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
如果我在清单和活动中删除 mimeType 行,我会使用发送自己的广播
Intent intent = new Intent("com.android.camera.NEW_PICTURE");
sendBroadcast(intent);
然后我成功接收到广播并且可以看到日志和 toast 窗口。我以正确的方式接近这个吗?有什么需要我补充的吗?