我需要在我创建的Service类中实现BroadcastReceiver:
public class MyService extends Service
Run Code Online (Sandbox Code Playgroud)
在这个类中,当用户按下实现sendBroadcas()的MyActivity类中的按钮时,我必须使用Thread - Sleep实现Simulation of Download.我无法将Service类扩展为BroadcastReceiver,因为它已经扩展到Service.任何人都可以帮我弄明白如何实现这种机制?
谢谢
android.intent.action.BOOT_COMPLETED如果我使用"重新启动"或"重新启动",则不会收到Android Intent ,但如果我关闭并打开设备,则会有效.有没有办法使这项工作?
这是我的Activity代码,
Long time = new GregorianCalendar().getTimeInMillis()+20000;//Setting alarm after 20 sec
Intent intentAlarm = new Intent("alarm");
intentAlarm.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intentAlarm.putExtra("req_code",10);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,10, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
Run Code Online (Sandbox Code Playgroud)
这些是我在我的应用中拥有的所有权限,
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="com.myapp.pack.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Run Code Online (Sandbox Code Playgroud)
这是我的BroadcastReceiver代码,
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences sharedPreferences =
context.getSharedPreferences( "mydata", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("elligible",true);
editor.apply();
}
Run Code Online (Sandbox Code Playgroud)
我BroadcastReceiver在清单上注册了我的,
<receiver …Run Code Online (Sandbox Code Playgroud) android android-intent android-alarms android-broadcast android-broadcastreceiver
MyReceiver.java
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
Log.i("MyReceiver", "MyAction received!");
}
}
Run Code Online (Sandbox Code Playgroud)
在AndroidManifest.xml(application标签下)
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="MyAction" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
MainActivity.Java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sendBroadcast(new Intent("MyAction"));
}
}
Run Code Online (Sandbox Code Playgroud)
MyReceiver.onReceive方法永远不会被触发.我错过了什么?
android broadcastreceiver android-manifest android-broadcast
所以我正在编写一个使用蓝牙发现设备的 Android 应用程序。这是我用来开始发现的代码。
try {
myBluetoothAdapter.startDiscovery();
Log.d("Bluetooth Started successfully","yes");
} catch (Error e) {
Log.d("FAILED","Ya failed mate");
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
然后,我注册一个 BroadcastReceiver 以监视何时找到设备。这是我的代码
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
final ArrayList<String> stringArrayList = new ArrayList<>();
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(getApplicationContext(),android.R.layout.simple_list_item_1,stringArrayList);
final ListView listView = findViewById(R.id.listView);
listView.setAdapter(arrayAdapter);
BroadcastReceiver myReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d("ACTION RECEIVED","Action was received");
Log.d("Device Name", String.valueOf(intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)));
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
stringArrayList.add(device.getName());
arrayAdapter.notifyDataSetChanged();
listView.invalidateViews(); …Run Code Online (Sandbox Code Playgroud) android runtime-error bluetooth broadcastreceiver android-broadcast
移动号码将由用户在我的Android应用程序的注册页面上以编辑文本输入.如何检查用户是否输入了他/她的手机号码?
我试过这个:
TelephonyManager tMgr =(TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
mPhoneNumber = tMgr.getLine1Number();
Run Code Online (Sandbox Code Playgroud)
并将此变量与edittext的文本进行比较.但是mPhoneNumber 在我的情况下返回NULL.还有其他选择吗?怎么解决这个?
任何帮助都会很明显.
我试过这个:检查源代码:
public class MainActivity extends Activity{
Button submit;
EditText contact;
String phNo;
ProgressDialog progress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contact = (EditText)findViewById(R.id.mobileNumber);
submit = (Button) findViewById(R.id.button1);
submit.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
phNo = contact.getText().toString();
new CheckOwnMobileNumber().execute();
Toast.makeText(getApplicationContext(), phNo, Toast.LENGTH_LONG).show();
}
});
}
private class CheckOwnMobileNumber extends AsyncTask<String, Void, String>
{
@Override
protected void onPostExecute(String result)
{
// TODO Auto-generated method stub …Run Code Online (Sandbox Code Playgroud) 我总是为我的问题找到以下答案:
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
Run Code Online (Sandbox Code Playgroud)
但它不适用于我的系统(Nexus4 Android 4. ...)
我可以创建一个文件并将其添加到Media-DB中
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(file);
mediaScanIntent.setData(contentUri);
context.sendBroadcast(mediaScanIntent);
Run Code Online (Sandbox Code Playgroud)
"file"是我要添加的新图像文件.
删除文件后,我尝试刷新库
Intent intent = new Intent(Intent.ACTION_MEDIA_MOUNTED);
Uri contentUri = Uri.parse("file://" + Environment.getExternalStorageDirectory());
intent.setData(contentUri);
context.sendBroadcast(intent);
Run Code Online (Sandbox Code Playgroud)
要么
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
Run Code Online (Sandbox Code Playgroud)
但是Galery仍有空占位符.
我不知道为什么?...
为了安全起见,我在AndroidManifest.xml中添加了我的Activity
<intent-filter>
<action android:name="android.intent.action.MEDIA_MOUNTED" />
<data android:scheme="file" />
</intent-filter>
Run Code Online (Sandbox Code Playgroud)
但结果是一样的.有什么想法解决这个问题吗?
我目前正在开发一个应用程序,只有当它是应用程序预期的SMS时才需要处理SMS(与Whatsapp在注册时的行为相同).
我想中止SMS Intent,因为它不会出现在SMS框中.
我的问题是:我知道谷歌改变了很多关于KitKat中的短信行为,现在,即使我的短信被我的应用程序很好地解析,短信也出现在SMSBox中,即使我this.abortBroadcast();在我的短信广播接收器中呼叫.那么有没有办法避免这些短信出现在短信框中而无需开发完整的短信应用程序?
有关信息,此广播接收器的清单文件中的优先级尚未达到1000(我也尝试使用MAX Integer).
在小米的MI设备上,有一个功能是在他们的安全应用程序中关闭/打开"自动启动".(在安全性应用程序 - >权限 - >自动启动)
这意味着当应用程序未运行时,任何广播接收器都不会收到任何内容.所以BOOT_COMPLETED,USER_PRESENT,CONNECTIVITY_CHANGE等......都行不通.(他们在应用程序处于前台后工作了一段时间,但很快就停止了).用户从小米的"最近的应用程序"版本中刷出应用程序后,它们也停止工作
即使GCM也无法将其唤醒
对于消息传递应用程序,这是一个杀手.
默认情况下,Whatsapp,Messenger,Flipkart等应用程序默认启用(即使这些应用程序未预先安装).
大多数其他应用程序默认禁用此功能.例如.默认情况下禁用Slack.
有没有办法默认进入这个白名单?
android android-service android-broadcast android-broadcastreceiver
我已经SMS Retriever API在谷歌教程和我的调试Build Variant工作中实现了类似的工作.我可以读取短信并获取用户可以登录的代码.
我的问题是,当我在发布版本Variant中运行应用程序sms它不起作用.我收到了短信,但我无法读取代码进行登录.
我在发布模式下更改了AppSignatureHelper生成的哈希,该模式与调试模式不同.在调试工作和发布没有.
一些帮助将是欣赏
代码:
表现:
<receiver android:name=".app.receivers.SmsReceiver">
<intent-filter>
<action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED"/>
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
在我的类中:(在发布和调试模式下,代码将抛出onSucess方法)此方法在onCreate中调用.
private void startSMSListening(){
SmsRetrieverClient client = SmsRetriever.getClient(this);
Task<Void> task = client.startSmsRetriever();
task.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
// Successfully started retriever, expect broadcast intent
Log.e("startSMSListening", "listening sms");
sendCode();
showHideLoadingView(false);
}
});
task.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Failed to start retriever, inspect Exception for more details
Log.e("startSMSListening", "failure listening …Run Code Online (Sandbox Code Playgroud) android android-intent android-broadcast android-broadcastreceiver android-sms