文档AlarmManagerstartes该
注意:警报管理器适用于您希望在特定时间运行应用程序代码的情况,即使您的应用程序当前未运行也是如此.对于正常的计时操作(刻度,超时等),使用Handler更容易,也更有效.
但是,一旦我的应用程序关闭(强制从任务管理器关闭),我的警报就不起作用,并且OnReceive永远不会在广播接收器内调用该方法.我的目标是4.x.
发生了什么?
寻找"最全面和兼容(即所有Android版本...)"方式来收听音量变化,我找到了两种不同的方法来处理这个问题:
哪种方法更可取?
为什么?
更新1:由于下面的评论,我发现onKeyDown()实际上接管了卷密钥,这可能不是一个完整的解决方案,因为其中一个帖子提到可以通过除硬件按钮之外的接口来更改卷(不是提到谷歌似乎正逐渐取消那些"接管"能力.
OTOH,android.media.VOLUME_CHANGED_ACTION是一个黑客,甚至没有记录.这可能意味着它将在Android 5左右停止工作 ......
更新2: registerMediaButtonEventReceiver根本不起作用!(对于音量硬件按钮,我只是尝试过).
其他见解?
android broadcastreceiver android-audiomanager android-broadcast
我试图使用BroadcastReceiver,但它不工作,请帮我解决这个问题.MyReceiver.java
package com.example.broadcast_receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("[BroadcastReceiver]", "MyReceiver");
if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
Log.i("[BroadcastReceiver]", "Screen ON");
}
else if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
Log.i("[BroadcastReceiver]", "Screen OFF");
}
}
}
Run Code Online (Sandbox Code Playgroud)
AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcast_receiver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name=".MyReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON"/>
<action android:name="android.intent.action.SCREEN_OFF"/>
</intent-filter>
</receiver>
<activity
android:name="com.example.broadcast_receiver.MainActivity"
android:label="@string/app_name" > …Run Code Online (Sandbox Code Playgroud) 我想知道在浏览器,消息等任何应用程序中选择文本时是否可以启动活动或应用程序.
就像我们在任何一个小弹出窗口中选择文本时提到剪切,复制,粘贴选项一样.我能在那里添加另一个按钮吗?启动我的申请?
如果我可以请指导我如何做到这一点并将数据发送到我的应用程序..
谢谢 !
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
Run Code Online (Sandbox Code Playgroud) 我需要在周围区域扫描蓝牙设备6至12秒.在此之后,我需要停止发现新设备.
以下代码应该:
问题是蓝牙发现永远不会被取消.在此代码运行一两分钟之后,onReceive将在同一秒内被调用数十次......
public void startTrackingButton(View view) {
Log.d("MAIN", "Track button pressed, isTracking: " + !isTracking);
if (isTracking) {
isTracking = false;
} else {
isTracking = true;
Thread keepScanning = new Thread(new Runnable() {
@Override
public void run() {
while (isTracking) {
if (mBluetoothAdapter.isDiscovering()) {
Log.d("MAIN", "Cancelling discovery!");
Log.d("MAIN", String.valueOf(mBluetoothAdapter.cancelDiscovery() + ":" + mBluetoothAdapter.getState()));
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}
startTracking();
try {
Thread.sleep(6000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} …Run Code Online (Sandbox Code Playgroud) 任何人都可以告诉我一种创建应用程序内BroadcastReceiver的方法吗?我创建了BroadcastReceiver,它可以播放一条消息.它甚至在应用程序处于后台状态时也能工作,我希望它只在应用程序处于前台时才能工作.
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.registerReceiver(this.mConnReceiver, new IntentFilter(
ConnectivityManager.CONNECTIVITY_ACTION));
}
private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
boolean noConnectivity = intent.getBooleanExtra(
ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
String reason = intent
.getStringExtra(ConnectivityManager.EXTRA_REASON);
boolean isFailover = intent.getBooleanExtra(
ConnectivityManager.EXTRA_IS_FAILOVER, false);
NetworkInfo currentNetworkInfo = (NetworkInfo) intent
.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
NetworkInfo otherNetworkInfo = (NetworkInfo) intent
.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);
if (currentNetworkInfo.isConnected()) {
System.out.println("Connected");
Toast.makeText(getApplicationContext(), "Connected",
Toast.LENGTH_LONG).show();
} else {
System.out.println("Not Connected");
Toast.makeText(getApplicationContext(), "Not Connected",
Toast.LENGTH_LONG).show();
} …Run Code Online (Sandbox Code Playgroud) 我创建的应用程序会在用户按下电源按钮3次后立即启动活动.经过一些研究,我发现要做到这一点,首先你需要创建一个服务,启动广播接收器来检查屏幕的开/关状态.
基本上我希望即使应用程序关闭也能运行.一旦按下电源按钮3次或更多次,它应该开始另一个活动.
如果您知道解决方案,请给我完整的代码或答案的链接.
我以某种方式设法编写此代码,但它无法正常工作:
这是我使用的服务类:
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class UpdateService extends Service {
BroadcastReceiver mReceiver;
@Override
public void onCreate() {
super.onCreate();
// register receiver that handles screen on and screen off logic
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
mReceiver = new MyReceiver();
registerReceiver(mReceiver, filter);
}
@Override
public void onDestroy() {
unregisterReceiver(mReceiver);
Log.i("onDestroy Reciever", "Called");
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
boolean screenOn = …Run Code Online (Sandbox Code Playgroud) 我正在尝试制作一个听取wifi改变广播并做一些动作的Android应用程序.但问题是当进程被杀死时它无法正常工作.我发现这个问题说没有活动就无法工作
如何在没有Activity/Service的情况下创建BroadcastReceiver?
所以这不是另类.因此我创建了一个空的活动.我不想创建一个在后台运行的服务.如果应用程序被杀死,我怎样才能让我的应用程序继续听.我已经在清单中注册了广播.
<activity
android:name=".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>
<receiver android:name="com.background.service.BroadCastReceiver">
<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
这是我的班级
public class BroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//do some action
}
}
Run Code Online (Sandbox Code Playgroud) 我是一个相当新的Android开发人员.我注意到的一件事是,当我走近/星巴克店内时,我的Android设备显示以下内容:

题:
我有以下情况:一个主要应用程序是启动器应用程序(A),应用程序始终运行,此应用程序调用另一个子应用程序(B)图1.当应用程序(A)启动intent service并且应用程序(B)使用此意图时(图2),会出现问题.
这是用于启动意向服务的代码:
服务
intentCodeRead.putExtra(BARCODE_TEXT, readStr);
intentCodeRead.putExtra(BARCODE_DATE, System.currentTimeMillis());
intentCodeRead.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
sendBroadcast(intentCodeRead);
Run Code Online (Sandbox Code Playgroud)
编辑1
在应用程序(A)(启动器应用程序)中,我有intentService一个broadcast用于另一个应用程序.的应用(B)是听这个广播的应用程序.当(A)进行广播时,应用程序(B)然后收听.问题是(A)在(A)做广播时带到(B)的前面.如何在不更改应用程序演示顺序的情况下进行更改?broadcast
编辑2
申请A.
BarcodeScannerService.java
intentCodeRead.putExtra(Constants.BARCODE_TEXT, readStr);
intentCodeRead.putExtra(Constants.BARCODE_DATE, System.currentTimeMillis());
intentCodeRead.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
sendBroadcast(intentCodeRead);
Run Code Online (Sandbox Code Playgroud)
initservice.java
Intent intentService = new Intent(context, BarcodeScannerService.class);
context.startService(intentService);
Run Code Online (Sandbox Code Playgroud)
申请B.
receive.java
BroadcastReceiver actionBarcodeScannerAndPresenceReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
/* Receiver for the barcode scan and presence sensor …Run Code Online (Sandbox Code Playgroud)