scheduling alarmmanager not working on reboot my phone - java

It's toasting until I didn't restart my phone but after restarting broadcastreceiver2 doesn't receive and nothing happens.
I followed http://stacktips.com/tutorials/android/how-to-start-an-application-at-device-bootup-in-android and many other but nothing happens.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.k2.alarmmanagerdemo">
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<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="com.k2.alarmmanagerdemo.MyBroadcastReceiver"
android:enabled="true">
</receiver>
<receiver android:name="com.k2.alarmmanagerdemo.BroadCastRecevier2"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
MainActivity.java
public class MainActivity extends Activity {
Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button) findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startAlert();
}
});
}
public void startAlert() {
EditText text = (EditText) findViewById(R.id.time);
int i = Integer.parseInt(text.getText().toString());
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),
1000 * 5,pendingIntent);
Toast.makeText(this, "Alarm set in " + i + " seconds",Toast.LENGTH_LONG).show();
}
}
MyBroadcastReceiver.java
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm...." + System.currentTimeMillis(), Toast.LENGTH_SHORT).show();
Log.i("Alarm.", "alarm called" + System.currentTimeMillis());
}
}
BroadCastRecevier2.java
public class BroadCastRecevier2 extends BroadcastReceiver {
MainActivity activity = new MainActivity();
#Override
public void onReceive(Context context, Intent intent) {
activity.startAlert();
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Intent i = new Intent();
i.setClassName("com.k2.alarmmanagerdemo",
"com.k2.alarmmanagerdemo.MainActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
Toast.makeText(context, "BOOT", Toast.LENGTH_SHORT).show();
Log.i("myboot","boot compleated inside");
}
}
}

Remove this line from your <receiver> declaration for BroadcastReceiver2:
android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
That line in the <receiver> tag is telling the system that only packages which have this permission are allowed to send your class the Intent. Its' likely confusing the system and preventing the Intent from being sent to your receiver.
You may also find this article helpful when learning about alarms and the boot complete receiver.

Related

How to set broadcast receiver attributes programmatically?

I'm broadcasting an intent in my app and receiving it with a broadcast receiver. I can handle the broadcasting and receiving. No problem with that. However, I want to register the receiver completely programmatically instead of doing it in the manifest file. Notice, that in the manifest file, there are two attributes of the receiver android:enabled="true" and android:exported="false". I need to know, how do I specifically set these two attributes when I register the receiver programmatically?
My AndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mybroadcastapplication">
<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/Theme.MyBroadcastApplication">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="false">
</receiver>
</application>
</manifest>
My MainActivity.java file:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
MyBroadcastReceiver myReceiver;
IntentFilter intentFilter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myReceiver = new MyBroadcastReceiver();
intentFilter = new IntentFilter();
intentFilter.addAction("com.example.mybroadcastapplication.EXPLICIT_INTENT");
findViewById(R.id.button1).setOnClickListener(this);
}
public void broadcastIntent() {
Intent intent = new Intent();
intent.setAction("com.example.mybroadcastapplication.EXPLICIT_INTENT");
getApplicationContext().sendBroadcast(intent);
}
#Override
protected void onPostResume() {
super.onPostResume();
registerReceiver(myReceiver, intentFilter);
}
#Override
protected void onStop() {
super.onStop();
unregisterReceiver(myReceiver);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
broadcastIntent();
break;
default:
}
}
}
My MyBroadcastReceiver.java file:
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() != null && intent.getAction().equals("com.example.mybroadcastapplication.EXPLICIT_INTENT"))
Toast.makeText(context, "Explicit intent received.", Toast.LENGTH_LONG).show();
}
}
Regards
You don't need to declare the BroadcastReceiver in the manifest if you are registering/unregistering programmatically. You only need to declare BroadcastReceivers in the manifest if you want them to be instantiated from external triggers (for example, on device boot, or from the Alarm Manager, etc.)

Alarm Manager does not work in background

I used alarm manager in my program,but for the first time I run it, It just worked when my program was open and when it was close it did not work in the background, but in the second time it worked in the background too.
MainActivity :
public class MainActivity extends AppCompatActivity {
AlarmManager alarmManager;
Intent intent;
PendingIntent pendingIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
intent = new Intent(MainActivity.this, AlertReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+60000, pendingIntent);
}
}
AlertReceiver :
public class AlertReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("Log", "onReceiver!!");
}
}
AndroidManifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.yadame">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<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" android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".AlertReceiver"/>
</application>

Alarm Manager not woking

I am using AlarmManager on clicking floatingActionButton but it is not calling the alarm class. I tested the floatingActionButton via Toasts but it is working fine but Toast and vibrator in My_Alarm Class is not working even tough i have enebled permission in Manifests. Kindly point out the problem!
Below code main activity (Alarm Manager in floatingActionButton)
floatingActionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, AlarmManager.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this,5,intent,0);
sendBroadcast(intent);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+1000,pendingIntent);
}
});
}
This is my Alarm class (Toast and Vibrate not working)
public class My_Alarms extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Fuck This Shit", Toast.LENGTH_LONG).show();
Vibrator v = (Vibrator)context.getSystemService(context.VIBRATOR_SERVICE);
v.vibrate(10000);
}
}
And this is my Manifest file (I have enabled permission)
<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=".Activity_Time_Setting"
android:grantUriPermissions="true"
></activity>
<activity android:name=".MainActivity">
android:grantUriPermissions="true"
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
android:grantUriPermissions="true"
</intent-filter>
</activity>
<receiver android:name=".My_Alarms"/>
</application>
Replace this line of code :
Intent intent = new Intent(MainActivity.this, AlarmManager.class);
with this :
Intent intent = new Intent(MainActivity.this, My_Alarms.class);
Also you don't need to use
sendBroadcast(intent);
this line because you are already passing intent in pending intent
At last try to to remove toast from your broadcast reciver

Monitoring an incoming SMS with Toast

I want to monitor an incoming sms in a phone by making a Toast message pop up when a sms is received. However, with the following code, I don't receive the Toast message even when a sms is received. Why is that so? I'm using Android Studio 3.0.1 and there are no errors when I run the code.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="mapp.com.sg.broadcastreceiver">
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS"></uses-permission>
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
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>
</application>
</manifest>
MainActivity.java
public class MainActivity extends Activity {
private static final int NOTIFY_ME_ID = 1337;
private BroadcastReceiver the_receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, "SMS Message Received!", duration);
toast.show();
}
};
private IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onResume() {
// Register receiver if activity is in front
this.registerReceiver(the_receiver, filter);
super.onResume();
}
#Override
protected void onPause() {
// Unregister receiver if activity is not in front
this.unregisterReceiver(the_receiver);
super.onPause();
}
}
Do as following by adding the receiver in the manifest file
<receiver
android:name=".receivers.SMSReceiver"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
and then create SMSReceiver class
public class SMSReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Bundle myBundle = intent.getExtras();
SmsMessage[] messages;
if (myBundle != null) {
Toast toast = Toast.makeText(context, "SMS Message Received!",duration);
}
}
}

getting action of buttons on receive() method which is a method of broadcast receiver but its show a null

I am getting action of buttons by getaction on receive() method which is a method of broadcast receiver but its sho a null plz tell me what I do in this to get a value which is not null
+my notify method is following
private void Notify(String notificationTitle, String notificationMessage)
{
String ns=Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager=(NotificationManager)getSystemService(ns);
#SuppressWarnings("deprecation")
Notification notification=new Notification(R.drawable.bg,"Time",System.currentTimeMillis());
RemoteViews notificationView=new RemoteViews(getPackageName(),R.layout.main);
Intent notificationIntent=new Intent(this,PlayerAudioActivity.class);
PendingIntent pendingNotificationIntent=PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentView=notificationView;
notification.flags|=Notification.FLAG_NO_CLEAR;
//supposed button call intent
Intent switchIntent=new Intent(this,MyReceiver.class);
PendingIntent pendingIntent=PendingIntent.getBroadcast(this, 0, switchIntent, 0);
notificationView.setOnClickPendingIntent(R.id.btnPrevious, pendingIntent);
notificationManager.notify(1, notification);
}
-my broadcastreceiver class is following
public class MyReceiver
extends BroadcastReceiver {
private static final String TAG = "waaaawoooooooooOOOOOOjnjkhdfku";
public void onReceive(Context context, Intent intent) {
final String action=intent.getAction();
// if(AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)){
Log.d(TAG, ""+action);
// Bundle extras=intent.getExtras();
// String ieString=extras.getString("Locale");
// try{
// final int Appwidgid=extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID);
// if(Appwidgid!=AppWidgetManager.INVALID_APPWIDGET_ID)
// {
// this.onDeleted(context, new int[] { Appwidgid });
// }
// else {
// onReceive(context, intent);
// }
// context.startService(new Intent(context,PlayerAudioActivity.class));
// Log.i(TAG,"Starting Service ConnectivityListener");
// }catch(Exception e){
// Log.e(TAG,e.toString());
// }
// }
}
private void onDeleted(Context context, int[] is) {
// TODO Auto-generated method stub
}
}
+manifest file is following
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Audio.audioplayer"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<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=".MyReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED"/><action
android:name="android.net.conn.CONNECTIVITY_CHANGE"/><action
android:name="android.net.conn.DATA_ACTIVITY_CHANGE"/>
</intent-filter>
</receiver>
<activity
android:name="com.Audio.audioplayer.PlayerAudioActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<data android:scheme="tel" />
</intent-filter>
</activity>
<activity
android:name=".PlayListActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.Audio.audioplayer.PlayerAudioActivity" />
</activity>
</application>
</manifest>
As adamp mentioned in this post you cannot access an action on an intent that you've not set:
{...}
//supposed button call intent
Intent switchIntent=new Intent(this, MyReceiver.class);
PendingIntent pendingIntent=PendingIntent.getBroadcast(this, 0, switchIntent, 0);
{...}
You have to add your designated action to that intent by using setAction().
Something like this:
{...}
Intent switchIntent=new Intent(this, MyReceiver.class);
switchIntent.setAction("YOUR_ACTION");
PendingIntent pendingIntent=PendingIntent.getBroadcast(this, 0, switchIntent, 0);
{...}
ps: just pseudo code

Categories

Resources