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
Related
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>
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.
I have some trouble ring an alarm on android-studio app. First, I have a method, called with a button ( onClick), then I create alarmManager in this, and try to call a class AlarmReceiver to ring the alarm. The problem is that the program running never go in my AlarmReceiver class, and I don't understand why. Let me show you the code:
public void sendMessage(View v) {
long time = currentTimeMillis();
Toast.makeText(this, "ALARM ON", Toast.LENGTH_SHORT).show();
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 14);
calendar.set(Calendar.MINUTE, 58);
Toast.makeText(MainActivity.this, "bjr", Toast.LENGTH_SHORT).show();
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),pendingIntent);}
This is the method called when click on button, and now this is the AlarmReceiver:
public class AlarmReceiver extends WakefulBroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Alarm! Wake up! Wake up!", Toast.LENGTH_LONG).show();
Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alarmUri == null)
{
alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
ringtone.play();
}
}
So, when I run the app, then click on button, the word " ALARM ON" and "bjr" is printed, but the text so in AlarmReceiver "Alarm! Wake up!" Is never printed, so the app never go in this class, I need to know why.
My android manifest :
`<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.beantunes.myapplication">
<uses-feature
android:name="android.hardware.sensor.accelerometer"
android:required="true"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".vue.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".vue.AlarmReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
<activity android:name=".vue.DisplayMessageActivity"></activity>
</application>
`
Hi everyone I'm studying now and developing a app in android studio. My problem is i can't proceed to my log in activity after splash screen. I already look in the net and stackoverflow and applied it, but still same error. I appreciate for the help and answer.
Here are some of my code
SplashScreen.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
final Intent mainIntent = new Intent(SplashScreen.this, Login.class);
SplashScreen.this.startActivity(mainIntent);
SplashScreen.this.finish();
}
},3000);
}
Login.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);
finish();
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/logo"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".SplashScreen"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".Login"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".Login"/>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="com.ex.app.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
where did i go wrong ?
As you have written code to move to MainActivity on the method onCreate of Login.java
Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);
finish();
Comment this line or keep it on the click of SignIn button
Your login activity just directs you to the MainActivity on create
Look at these lines
Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);
It is redirecting to MainActivity directly as in your LoginActivity onCreate you have written
Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);
finish();
without any condition. Make it conditional by putting your login check conditions and criteria
if (<logged_in_your_condition_Check>){
Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);
finish();
}
why you require below lines in Login.class
Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);
finish();
if you are using finish() in onCreate() then it will immediately called onDestroy() and that dosen't make any sense.
remove above given code from onCreate your code will work
in manifest write:
<activity
android:name=".MySplashActivity"
android:noHistory="true"
..... >
....
</activity>
then in create() or somewhere different cal the method:
private static int SPLASH_TIME_OUT=2000;
private void nextScreen() {
new Handler().postDelayed(()-> {
Intent intent = new Intent(MySplashaActivity.this, NextActivity.class);
startActivity(intent);
finish();
}, SPLASH_TIME_OUT);
}
I have different Ongoing notifications. When an addAction() button is pressed a BroadcastReceiver is called and my notification is cleaned. The problem is the addAction() always pass the id of the last notification added, not the one that you pressed:
Here is the method adding the notification:
public void pushNotification(View v){
String text = editText.getText().toString().trim();
editText.setText("");
int millis = (int) (System.currentTimeMillis() % Integer.MAX_VALUE);
Intent notificationIntent = new Intent(mContext, NotificationReceiver.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.putExtra("id", millis);
PendingIntent pIntent = PendingIntent.getBroadcast(mContext, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notification = new NotificationCompat.Builder(mContext);
notification.setContentTitle("");
notification.setContentText(text);
notification.setSmallIcon(R.mipmap.ic_launcher);
notification.setOngoing(true);
notification.addAction(R.mipmap.ic_launcher, "Dismiss", pIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(millis, notification.build());
}
Receiver:
public class NotificationReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent){
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(intent.getIntExtra("id", 0));
}
}
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="PACKAGE_NAME">
<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"
android:label="#string/app_name"
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=".NotificationReceiver" />
</application>
I found the solution, just add FLAG_ONE_SHOT to your pending intent and now all the extras will be different.