Tag: broadcastreceiver

用于在Android中启动新Activity的多个Intent标志

我试图在Intent中添加更多的标志,以便在BroadcastReceiver中启动一个新的Activity,响应从应用程序的另一部分发送的特定意图。 我已经添加,因为我注意到了LogCat消息在我创建的意图FLAG_ACTIVITY_NEW_TASK标志,然后其他的,但我在LogCat中得到相同的错误,因为FLAG_ACTIVITY_NEW_TASK不在那里。 代码如下: public class actReceiver extends BroadcastReceiver { … public void onReceive(Context context, Intent intent) { … else if (intent.getAction().equals(“something”)) { Intent prefAct = new Intent(context, PreferencesActivity.class) prefAct.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) .setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) .setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY) .setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); startActivity(prefAct); } … } … } 而特定的LogCat错误: Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is […]

在kotlin广播Reciver

如何在Kotlin中使用注册并在Android中创建Broadcast Receiver。 任何建议….在Java中,您可以通过声明它为广播接收器来创建它。但是在Kotlin中没有广播接收器function…以及如果有,那么我无法find它或如何使用它。

在Android上使用Wi-Fi创建P2P连接:创建BroadcastReceiver类

我正尝试在Android上创建Wi-Fi P2P连接。 我指的是API https://developer.android.com/training/connect-devices-wirelessly/wifi-direct.html,但我坚持创建BroadcastReceiver类。 它说:“现在创建一个新的BroadcastReceiver类,用来监听系统的Wi-Fi P2P状态的变化。在onReceive()方法中,添加一个条件来处理上面列出的每个P2P状态变化。 @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) { // Determine if Wifi P2P mode is enabled or not, alert // the Activity. int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1); if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) { activity.setIsWifiP2pEnabled(true); } else { activity.setIsWifiP2pEnabled(false); } } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) […]

Kotlin AlarmManager和BroadcastReceiver无法正常工作

我试图用AlarmManager设置闹钟,但我的BroadcastReceiver从来没有被调用。 这是我的片段。 val receiver = object : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { //Never gets hit } } context.registerReceiver(receiver, IntentFilter(LOCAL_NOTIFICATION)) val intent = Intent() intent.action = LOCAL_NOTIFICATION val alarmManager = context.getSystemService(ALARM_SERVICE) as? AlarmManager val pendingIntent = PendingIntent.getService(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT) val calendar = Calendar.getInstance() calendar.add(Calendar.SECOND, 10) alarmManager?.set(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent) 我试过在AndroidManifest.xml中注册一个广播接收器,但似乎没有任何工作。

Kotlin:调用函数从BroadcastReceiver onReceive更新UI

我是Kotlin新手,看起来很棒! 尽管今天,我一直在尝试在Java中做一些超级简单的事情,但是我完全陷入了困境。 我正在使用广播接收器来确定设备何时与电源连接/断开。 而我所需要做的就是相应地更新我的用户界面。 我的代码 这是我的BroadcastReceiver类,它似乎工作正常。 class PlugInReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { val action = intent.action if (action == Intent.ACTION_POWER_CONNECTED) { // Do stuff when power connected } else if (action == Intent.ACTION_POWER_DISCONNECTED) { // Do more stuff when power disconnected } } } 现在在我的MainActivity (但其他地方,稍后),我想更新我的用户界面时,意图被激发,例如在下面的函数,背景颜色的变化。 private fun updateBackgroundColor( […]

在Kotlin的BroadcastReceiver不起作用

使用本手册http://www.techotopia.com/index.php/Kotlin_Android_Broadcast_Intents_and_Broadcast_Receivers#.EF.BB.BFSummary我已经在Kotlin中实现了BroadcastReceiver,所以我期望在重新启动后,应用程序将启动,但它不会。 请帮忙。 谢谢! 广播接收器 class BroadcastReceiverOnBootComplete : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { if (intent.action.equals(Intent.ACTION_BOOT_COMPLETED, ignoreCase = true)) { val message = “Broadcast intent detected ” + intent.action Toast.makeText(context, message, Toast.LENGTH_LONG).show() } } } 清单文件 MainActivity与BroadcastReceiver class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { private var launchers = ArrayList() private var mStoredPrimaryColor = 0 […]

广播接收器onReceive()不被调用

广播接收机 mBroadcastReceiver = object : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { Log.i(TAG,"contact list populate broadcast") var action = intent.action Log.v("Action is ",action) when (action) { GetContactListFromServer -> { println("Get contact list") } } } } val filter = IntentFilter(GetContactListFromServer) this.registerReceiver(mBroadcastReceiver, filter) 这是我如何发送广播 val i = Intent(ContactListActivity.Obz.GetContactListFromServer) i.`package` = mApplicationContext?.getPackageName() mApplicationContext?.sendBroadcast(i) 此代码工作正常,直到昨晚。 我无法调试出了什么问题?

Parse.com推送通知延迟或仅在Android应用程序重新启动后

当测试parse.com推送通知(从afterCave上的CloudCode发送)时,有一些奇怪的事情。 有时Android应用程序会立即得到通知(低于1秒),但有些时候会延迟多秒。 重新启动应用似乎会导致尚未收到的通知立即出现。 可能是什么原因? 这可能是一个错误,例如,parse.com服务? 可以发送或接收多少通知(每单位时间)是否有限制? 问题同时发生在自定义的BroadcastReceiver和默认的系统栏通知上。 服务器端JavaScript CloudCode: Parse.Cloud.afterSave("Timer", function(request) { // from https://www.parse.com/docs/js/guide#cloud-code console.log("Before Parse.Push.send — without alert"); var query = new Parse.Query(Parse.Installation); // http://blog.parse.com/announcements/pushing-from-the-javascript-sdk-and-cloud-code/ : Parse.Push.send({ where: query, data: { //alert: "afterSave on a Timer — Parse.Push.send" } }); console.log("After Parse.Push.send — without alert"); }); Kotlin中的自定义广播接收器(但问题也没有自定义BroadcastReceiver): override fun onCreate(savedInstanceState: Bundle?) { super<BaseActivity>.onCreate(savedInstanceState) […]

是否有可能通过intent-filter和broadcastreciever获取剪贴板管理器?

是否有可能通过意向过滤器和广播接收机复制文本? 我想开发一个字典应用程序,当用户在他手机中的任何位置复制文本时,我想显示文本的含义。 目前我已经尝试使用基本活动来注册监听,但我不符合我的需要。 mClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); listener = new ClipboardManager.OnPrimaryClipChangedListener() { public void onPrimaryClipChanged() { performClipboardCheck(); } }; mClipboard.addPrimaryClipChangedListener(listener); 谢谢。

BroadcastReceiver通过ACTION_SEND处理文件?

Android中是否可以创建BroadcastReceiver来处理从其他应用程序共享的文件? 在清单中我写道: <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <receiver android:name=".modules.file.FileReceiver" > <intent-filter> <category android:name="android.intent.category.DEFAULT" /> <action android:name="android.intent.action.SEND" /> <action android:name="android.intent.action.SEND_MULTIPLE" /> <data android:mimeType="application/pdf" /> </intent-filter> </receiver> 和我的FileReceiver文件(kotlin): class FileReceiver : WakefulBroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { context?.startService(Intent(context, FileHandlingIntentService::class.java)) } } 但我不能发送文件从文件管理器到我的应用程序,因为我的应用程序不在应用程序的列表中可以处理文件…是否真的可以处理文件只在活动? 有什么建议么?