我正在开发一个用于检查互联网连接的Android广播接收器.
问题是我的广播接收器被调用了两次.我希望它只在网络可用时才被调用.如果它不可用,我不希望得到通知.
这是广播接收器
public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
final ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifi.isAvailable() || mobile.isAvailable()) {
// Do something
Log.d("Network Available ", "Flag No 1");
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcastreceiverforinternetconnection"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name=".NetworkChangeReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" …Run Code Online (Sandbox Code Playgroud) java android broadcastreceiver android-networking android-wifi
我的班级必须实施哪个监听器,以便在wifi连接/断开连接时自动检查代码?
我能够手动检查wifi连接/断开连接,但每次我需要从Android设置连接/断开WIFI,然后运行我的程序以获得结果.
我目前的代码很简单:
WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
if (wifi.isWifiEnabled()==true)
{
tv.setText("You are connected");
}
else
{
tv.setText("You are NOT connected");
}
Run Code Online (Sandbox Code Playgroud) 我将从服务广播发送到活动时遇到问题.
这就是我在Service类中的内容:
Intent intent = new Intent();
intent.setAction(BROADCAST_ACTION);
sendBroadcast(intent);
Run Code Online (Sandbox Code Playgroud)
我有很多活动,在我的一项活动中我有这个:
class MyBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context ctxt, Intent i) {
System.out.println("received");
}
};
Run Code Online (Sandbox Code Playgroud)
我的问题是我的广播接收器没有收到任何东西!
救命!
编辑:
如果我有很多活动,怎么可以向所有人发送广播消息.换句话说,我可以将相同的广播接收器应用于所有活动!
我想通过广播动态注册和取消注册我的接收器类:"android.net.wifi.STATE_CHANGE"如果我在清单中执行此操作,这非常有效.但这使它变得静止.我想在activity类中动态地完成它.它在活动类中的通讯命令是什么?
这就是我的代码......而且由于注册和取消注册(多次)我的接收器(它正在启动服务),我遇到了问题.
public class startScreen extends Activity {
/** Called when the activity is first created. */
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.example.MyService");
context.startService(serviceIntent);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.initial);
final IntentFilter filter = new IntentFilter();
filter.addAction("android.net.wifi.STATE_CHANGE");
Button button = (Button) findViewById(R.id.button1);
final ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton1);
try {
...some code...
if (bool == true) {
toggleButton.setChecked(true);
this.registerReceiver(receiver, filter);
} …Run Code Online (Sandbox Code Playgroud) 我有一个SmsReceiver课程,我想在主要活动中注册,我应该怎么做?
我是安卓新手。
我正在编写应用程序,它基于GPS收集位置数据.我有下一个问题:当我尝试获取GPS数据并关闭GPS时,我会显示要求打开GPS的通知,然后以"ACTION_LOCATION_SOURCE_SETTINGS"开始(当然点击)意图.问题:我怎么知道用户打开了它?是否有一些广播的动作,或者我可以设置一些听众,或其他什么?
我试图registerReceiver像BluetoothDevice类的事件ACTION_ACL_CONNECTED,ACTION_ACL_DISCONNECTED.
但是我使用的类registerReceiver不扩展Activity类或Service类 - 所以我传递Context给那个类并按如下方式注册接收器
context.registerReceiver(ActionFoundReceiver,
new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED));
context.registerReceiver(ActionFoundReceiver,
new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED));
context.registerReceiver(ActionFoundReceiver,
new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED));
Run Code Online (Sandbox Code Playgroud)
和处理事件如下
private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
// do some stuff
}
}
};
Run Code Online (Sandbox Code Playgroud)
但是当设备连接或断开连接时我无法跟踪是否意味着ActionFoundReceiver无法调用
我对 android 和 java 真的很陌生。我正在制作一个应用程序,它有一个媒体服务,我希望媒体在来电时停止或暂停。这是我的媒体服务代码
公共类 ServiceMusic 扩展服务 {
MediaPlayer music;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
music.stop();
music.release();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
if (music !=null)
music.stop();
music = MediaPlayer.create(ServiceMusic.this, R.raw.music);
music.start();
music.setLooping(true);
}
Run Code Online (Sandbox Code Playgroud)
}
如果您能帮助我,我将非常感激,如果您能给我一个完整的指导,那就太好了。谢谢
我设法让我的耳机按钮在按下时被我的应用程序识别,但其中一个按钮需要调用 MyCustomActivity 中的方法。问题是 onReceive 的第一个参数是一个无法转换为 Activity 的上下文,因此我被迫将 BroadcastReceiver 实现为 MyCustomActivity 中的内部类。
到目前为止一切顺利,但如何在清单中注册这个内部 MediaButtonEventReceiver?
对于独立类,这很简单:
<receiver android:name=".RemoteControlReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
对 MyCustomActivity 的 mReceiver 执行相同操作的技巧/语法是什么?
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context ctx, Intent intent) {
// ...
}
}
Run Code Online (Sandbox Code Playgroud) 我目前正在为 Android 编写几个支持 NFC 的应用程序,想知道如何阻止我的应用程序出现在从启动器或非 NFC 应用程序扫描标签时打开的“选择应用程序”列表中。我只希望我的应用程序在打开时能够读取标签。
我目前的意图过滤器:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="@xml/nfc_tech_filter" />
Run Code Online (Sandbox Code Playgroud)
nfc_tech_filter.xml:
<tech-list>
<tech>android.nfc.tech.MifareUltralight</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.Ndef</tech>
</tech-list>
Run Code Online (Sandbox Code Playgroud) 我有动态注册BroacasrReceiver的问题CallReceiver.当trigerred onReceive方法时,CallReceiver抛出致命异常.
...
private boolean isCRRegistered = false;
public int onStartCommand (Intent intent, int flags, int startId) {
if(!isCRRegistered) {
IntentFilter filter2 = new IntentFilter("android.intent.action.PHONE_STATE");
CallReceiver mCallReceiver = new CallReceiver();
registerReceiver(mCallReceiver, filter2);
//registering some listener
mCallReceiver.registerListener(this);
}
}
Run Code Online (Sandbox Code Playgroud)
注销:
if(isCRRegistered) {
unregisterReceiver (mCallReceiver);
}
Run Code Online (Sandbox Code Playgroud)
logcat的:
02-04 14:41:50.286: E/AndroidRuntime(28328): FATAL EXCEPTION: main
02-04 14:41:50.286: E/AndroidRuntime(28328): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.PHONE_STATE flg=0x20000000 (has extras) } in com.kris.intellignetringer.CallReceiver@448162b8
02-04 14:41:50.286: E/AndroidRuntime(28328): at android.app.ActivityThread$PackageInfo$ReceiverDispatcher$Args.run(ActivityThread.java:934)
02-04 14:41:50.286: E/AndroidRuntime(28328): …Run Code Online (Sandbox Code Playgroud) android ×11
android-wifi ×2
intentfilter ×2
dynamic ×1
java ×1
media ×1
nfc ×1
service ×1
wifi ×1