我正在尝试在触发警报时在通知栏中显示通知.以下是我的代码.
AlarmReciever.java
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;
import android.util.Log;
import android.widget.Toast;
/**
* Created by Yohan on 6/29/2016.
*/
public class AlarmReciever extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
// TODO Auto-generated method stub
// here you can start an activity or service depending on your need
// for ex you can start an activity to vibrate phone or to ring the phone
String message="Hi I will …Run Code Online (Sandbox Code Playgroud) java android android-notifications android-notification-bar android-broadcastreceiver
我正在Android应用程序中实现谷歌Awareness API,但没有一个示例,也没有指南,显示如何在应用程序关闭或在后台收听API事件.我根据这里的答案写了一个全球接收器
<receiver android:name=".MyFenceReceiver" >
<intent-filter>
<action android:name="android.intent.action.FENCE_RECEIVER_ACTION" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
但是,由于它不起作用,我怀疑我不知道用于拦截Awarness事件的正确意图过滤器.有没有人知道正确的意图过滤器,或者如果这不是我的问题,我如何在应用程序关闭时或在全局接收器的后台拦截API事件?
android intentfilter android-broadcastreceiver google-awareness
我正在尝试在音乐播放服务运行时构建通知,并使用通知使用广播机制与服务进行交互(播放,暂停,停止).
(我知道也有可能在通知中使用PendingIntent.getService()作为操作按钮,但我不喜欢这个想法,因为这会触发服务的onStartCommand(),我需要解析和分析Intent对象采取行动,看起来不像BroadcastReceiver那样干净,如下所述.
让我们用一些(截断的)代码来说明我们到目前为止所拥有的内容.
我们在服务生命周期内创建Notification对象,添加操作按钮,并使用显示通知startForeground().
...
Intent i = new Intent(getBaseContext(), PlayerService.class);
PendingIntent piStop = PendingIntent.getBroadcast(getBaseContext(), 1, i, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Action actionStopPlayback = new NotificationCompat.Action(R.drawable.ic_stop_white_36dp, "Stop playback", piStop);
notification.addAction(actionStopPlayback);
...
Run Code Online (Sandbox Code Playgroud)然后我们在服务的onCreate()中注册一个BroadcastReceiver(当然在onDestroy中取消注册它;这是一个更简化的例子).
IntentFilter intentFilter = new IntentFilter();
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(getClass().toString(), "Broadcast received");
}
}, intentFilter);
Run Code Online (Sandbox Code Playgroud)最后的结果是接收器的onReceive()永远不会被调用.该服务是连续的,并且在通知操作发送广播时处于活动状态.由于我的性质无法调试广播,我在这里被封锁了.
android android-service android-notifications android-broadcastreceiver
我想使用广播接收器在每次活动中连续检查互联网连接。我已经编写了代码,它可以正常工作。但我想在每个活动中都使用它。如何修改此代码?
public class MainActivity extends Activity {
private static final String LOG_TAG = "CheckNetworkStatus";
private NetworkChangeReceiver receiver;
private boolean isConnected = false;
private TextView networkStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
receiver = new NetworkChangeReceiver();
registerReceiver(receiver, filter);
networkStatus = (TextView) findViewById(R.id.networkStatus);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
protected void onDestroy() { …Run Code Online (Sandbox Code Playgroud) 我已经为电话状态更改创建了广播接收器。但广播无法正常工作。我已经尝试了几个小时并尝试了2,3个解决方案,但仍然无法正常工作。互联网上的其他人具有相同的代码,对于他们来说工作正常。我不知道我在哪里犯错。需要你的帮助!这是我的清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="veclar.map.callandsmsblocking">
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".PhoneCallReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
这是我的PhoneCallReceiver类
public abstract class PhoneCallReceiver extends BroadcastReceiver {
//The receiver will be recreated whenever android feels like it. We need a static variable to remember data between instantiations
private static int lastState = …Run Code Online (Sandbox Code Playgroud) android telephony broadcastreceiver telephonymanager android-broadcastreceiver
我想声明一个广播接收器,它可以监听系统广播,例如PACKAGE_ADDED,,,PACKAGE_REPLACED例如
<receiver
android:name="com.sample.cli.xyz.XyzReceiver"
android:exported="true"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
如果我保留exported="true"在这里,任何应用程序都可以发送广播,这可能是一个安全问题。根据 Android 文档,如果我们在接收器标记中甚至有 1 个意图过滤器,那么导出的默认值将被视为“true”。
我的问题是,如果我明确将此属性声明为“false”( android:exported="false") 以及意图过滤器,是否会使其更安全并使其只能由系统而不是其他应用程序访问?
android broadcastreceiver android-broadcast android-broadcastreceiver
我的片段中有一个回收器视图,每次在广播接收器中发生推送消息时,我都想更新我的回收器视图。我的问题是当接收器调用时,它用前一个项目替换新项目,但我想创建新行并且以前的项目仍然存在. 我搜索了很多,但没有成功。这是我的片段,其中包含广播接收器
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View drawer = inflater.inflate(R.layout.fragment_notif_list, container, false);
rv = (RecyclerView) drawer.findViewById(R.id.recyclerview);
rv.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getActivity());
rv.setLayoutManager(mLayoutManager);
prefs = getActivity().getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Config.PUSH_NOTIFICATION)) {
// new push notification is received
Toast.makeText(getActivity(), "Push notification is received!", Toast.LENGTH_LONG).show();
message = intent.getStringExtra("message");
title = intent.getStringExtra("title");
timestamp = intent.getStringExtra("timestamp");
Log.d("message -->", message);
Log.d("title -->", title);
Log.d("time -->", timestamp);
MessageObj messageObj = …Run Code Online (Sandbox Code Playgroud) 我正在尝试在接到电话后显示吐司,我已经实现了注册广播接收器所需的所有必要的东西,但它没有显示吐司.我想在Marshmallow设备上运行这个程序
MyCallReceiver.java -
package com.suhas.callreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
public class MyCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
// This code will execute when the phone has an incoming call
// get the phone number
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Toast.makeText(context, "Call from:" +incomingNumber, Toast.LENGTH_LONG).show();
Log.d("MyTrack call", "call receive");
} else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_IDLE))
{
Toast.makeText(context, "Detected call hangup event", Toast.LENGTH_LONG).show();
}
else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals( …Run Code Online (Sandbox Code Playgroud) 我想创建一些具有不同时间的警报,并在选择每个警报时将它们一个接一个地取消。我可以创建几个警报。但是我不知道如何选择它们。
目前,取消警报的程序正在处理我放置的最后一个警报。
这是我的工作
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
TimePicker myTimePicker;
Button buttonstartSetDialog;
Button buttonCancelAlarm;
TextView textAlarmPrompt;
Button buttonstartSetDialogOne;
Button buttonstartSetDialogTwo;
Button buttonstartSetDialogThree;
TextView textAlarmPromptOne;
TextView textAlarmPromptTwo;
TextView textAlarmPromptThree;
TimePickerDialog timePickerDialog;
int RQS_1 = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textAlarmPrompt = (TextView) findViewById(R.id.alarmprompt);
textAlarmPromptOne = (TextView) findViewById(R.id.alarmpromptOne);
textAlarmPromptTwo = (TextView) findViewById(R.id.alarmpromptTwo);
textAlarmPromptThree …Run Code Online (Sandbox Code Playgroud) java android alarmmanager android-pendingintent android-broadcastreceiver
我正在尝试制作一个简单的应用程序,该应用程序将通知是否有可用的互联网连接,以及互联网连接更改。我在互联网上找到了一些解决方案,并尝试实施它们,但是不知何故,它不起作用。我已在清单文件中注册的广播接收器未调用网络连接更改。
表现
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".NetworkStateChangeReceiver">
<intent-filter >
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
</application>
Run Code Online (Sandbox Code Playgroud)
广播接收器
package com.gdm.internetconnectivitycheck;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import static android.content.Context.CONNECTIVITY_SERVICE;
public class NetworkStateChangeReceiver extends BroadcastReceiver {
public static final String NETWORK_AVAILABLE_ACTION = "com.gdm.retailalfageek.NetworkAvailable";
public static final String IS_NETWORK_AVAILABLE = "isNetworkAvailable";
@Override …Run Code Online (Sandbox Code Playgroud)