Simplest way to get to `BroadcastReceiver` with `AlarmManager` not working - java

Suppose I have a MainActivity.java, where if I press a button(id=get_button), I will go to the onReceive() of NotificationReceiver.java 2 minutes later :
But I am not going there. As for this and this and many other resources I googled, This seems to be the right way.
My MainActivity.java :
package com.example.insanes.chothavandar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import java.util.Calendar;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.get_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(MainActivity.this, NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
MainActivity.this,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE) + 2);
cal.set(Calendar.SECOND, 0);
am.setRepeating(
AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(),
AlarmManager.INTERVAL_DAY,
pendingIntent);
}
});
}
}
My NotificationReceiver.java :
package com.example.insanes.chothavandar;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class NotificationReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Not logging after 2 minutes
// Am I doing something wrong?
Log.d("DEBUG-EXISTENSE", "Reached in the broadcastreceiver");
}
}
I have registered the receiver in the manifest:
My menifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.insanes.chothavandar">
<uses-permission android:name="android.permission.INTERNET" />
<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=".NotificationReceiver"/>
</application>
</manifest>

you want to write this code in your manifest file.
ex :
android:name="com.example.insanes.chothavandar.NotificationReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.example.insanes.chothavandar.SHOW_NOTIFICATION" />
</intent-filter>
</receiver>
If you will go to the onReceive() of NotificationReceiver.java 2 minutes later so, you will use a handler in your class.
ex:
private Handler mStatusHandler = new Handler();
private Runnable mStatusRunnable;
private void checkStatus() {
mStatusRunnable = new Runnable() {
#Override
public void run() {
// Hear right your on button click code.
checkStatus();
}
};
mStatusHandler.postDelayed(mStatusRunnable, 2000);
}

i don't know about the right way or not , but this can be done via Handler in pretty simple way,
int TIME=2000
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// do stuff
}
}, TIME);

Related

Android: Unable to find explicit activity class even though I defined it in AndroidManifest.xml?

I am writing a simple application that triggers an alarm 5 seconds after the app launch, in which another activity (AlarmDialog) is launched. However, when I run my app, I get the following error:
Caused by: android.content.ActivityNotFoundException: Unable to find
explicit activity class {com.example.basicalarmsetter/AlarmDialog};
have you declared this activity in your AndroidManifest.xml?
This seems strange, considering that I believe I have declared the activity in my AndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.basicalarmsetter">
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<application
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".AlarmDialog"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
</activity>
<receiver android:name=".AlarmReceiver"/>
</application>
</manifest>
Below is my code for the app's other classes:
MainActivity.java:
package com.example.basicalarmsetter;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends Activity {
private Context context;
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
// Creates the Pending Intent used to set off the alarm
private PendingIntent generateAlarmPendingIntent(Context context) {
Intent intent = new Intent(context, AlarmReceiver.class);
// Each alarm requires a unique id
int alarmId = (int) (Math.random() * (10000 - 1 + 1) + 1);
System.out.println("alarmId = " + alarmId);
PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(context, alarmId, intent, 0);
return alarmPendingIntent;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
// Set an alarm 5 seconds after now
alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmMgr.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000,
generateAlarmPendingIntent(context));
}
}
AlarmReceiver:
package com.example.basicalarmsetter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context k1, Intent k2) {
System.out.println("Alarm received!");
Intent i = new Intent();
i.setClassName("com.example.basicalarmsetter", "AlarmDialog");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
k1.startActivity(i);
}
}
AlarmDialog:
package com.example.basicalarmsetter;
import android.app.Activity;
import android.os.Bundle;
public class AlarmDialog extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alarm_dialog);
}
}
File Structure: All classes MainActivity, AlarmDialog, and AlarmReceiver, are under the same directory.
Replace:
Intent i = new Intent();
i.setClassName("com.example.basicalarmsetter", "AlarmDialog");
with:
Intent i = new Intent(k1, AlarmDialog.class);
This will use a better constructor, one that helps avoid the mistake in your hard-coded strings. As a bonus, if you use an IDE to rename the AlertDialog class, your Intent will be modified as well.

Android 8 handleReceiver java.lang.RuntimeException

I know than this bug was commented in other blogs but I've tried to correct this and I did not succeed.
Only happend in android 8+, but in simulator of Android Studio works fine.
I have an app than activate a notification in some specific date, this notification remember some task for user. This works fine in android 7 or lower but in Android 8+ don't works. App works fine, but notification don't appear and in google Play is activated an ERROR.
My app never crash, only shows that error into Google Play console.
This is the bug in PlayStore Console:
java.lang.RuntimeException:
at android.app.ActivityThread.handleReceiver (ActivityThread.java:3606)
at android.app.ActivityThread.access$1300 (ActivityThread.java:237)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1796)
at android.os.Handler.dispatchMessage (Handler.java:106)
at android.os.Looper.loop (Looper.java:214)
at android.app.ActivityThread.main (ActivityThread.java:7045)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:964)
Caused by: java.lang.IllegalStateException:
at android.app.ContextImpl.startServiceCommon (ContextImpl.java:1666)
at android.app.ContextImpl.startService (ContextImpl.java:1611)
at android.content.ContextWrapper.startService (ContextWrapper.java:677)
at android.content.ContextWrapper.startService (ContextWrapper.java:677)
at com.example.tomas.memoru.R_Activate.onReceive (R_Activate.java:45)
at android.app.ActivityThread.handleReceiver (ActivityThread.java:3597)
And this is my source code:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tomas.memoru" >
<permission android:name="com.example.tomas.memoru.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:name="com.example.tomas.memoru.R_Variables1"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light.DarkActionBar" >
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<receiver
android:name=".R_Activate">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<service android:name="com.example.tomas.memoru.R_Activate_generador" />
<service android:name="com.example.tomas.memoru.GcmIntentService" />
<receiver android:name="com.example.tomas.memoru.R_NotificationPublisher" />
<activity
android:name="com.example.tomas.memoru.R_Pantalla_Main"
android:screenOrientation="portrait"
android:configChanges="orientation|screenSize"
android:launchMode="singleInstance"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
R_Activate.java
package com.example.tomas.memoru;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import java.util.ArrayList;
public class R_Activate extends BroadcastReceiver {
Integer cont1 = 0;
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
intent = new Intent(context,R_Activate_generador.class);
context.startService(intent);
Log.i("Autostart", "started");
}
}
}
R_Activate_generator.java
package com.example.tomas.memoru;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.IBinder;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;
public class R_Activate_generador extends Service {
ArrayList<String> task_cargar = new ArrayList<>();
Integer cont1=0;
static final int READ_BLOCK_SIZE = 100;
private static final String TAG = "MyService";
#Override
public IBinder onBind(Intent intent) {
return null;
}
public void onDestroy() {
super.onDestroy();
stopForeground(true);
}
#Override
public void onStart(Intent intent, int startid) {
cargadatosfile(); //this works fine, only charge information to check date and tasks
if(cont1>0){
scheduleNotification(getNotification("30 second delay"), 0);//this put notification
}
}
private void cargadatosfile() {
//this function works fine, only charge information to schedule the tasks
}
private void scheduleNotification(Notification notification, int delay) {
R_Variables1 comparteVariables = ((R_Variables1) getApplicationContext());
String s = "";
Integer temp_id = 0, masXdias=0;
String[] strValues_temp1;
Calendar today = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
cont1 = 0;
while (cont1 < task_cargar.size()) {
strValues_temp1 = task_cargar.get(cont1).split(";");
String[] strValues_time1 = strValues_temp1[11].split(" ");
String[] strValues_time2 = strValues_time1[1].split(":");
String[] strValues_date1 = strValues_time1[0].split("-");
Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
cal.set(Calendar.DATE, Integer.valueOf(strValues_date1[2]) + masXdias); //1-31
cal.set(Calendar.MONTH, Integer.valueOf(strValues_date1[1]) - 1); //first month is 0!!! January is zero!!!
cal.set(Calendar.YEAR, Integer.valueOf(strValues_date1[0]));//year...
cal.set(Calendar.HOUR_OF_DAY, Integer.valueOf(strValues_time2[0])); //HOUR
cal.set(Calendar.MINUTE, Integer.valueOf(strValues_time2[1])); //MIN
cal.set(Calendar.SECOND, 0); //SEC
if (cal.after(today) ) {//fecha futura es la de File
Intent notificationIntent = new Intent(this, R_NotificationPublisher.class);
notificationIntent.putExtra(R_NotificationPublisher.NOTIFICATION_ID, Integer.valueOf(strValues_temp1[0]));// se reemplazo 1 por max_id
// notificationIntent.putExtra(R_NotificationPublisher.NOTIFICATION, notification);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, Integer.valueOf(strValues_temp1[0]), notificationIntent, PendingIntent.FLAG_ONE_SHOT);//FLAG_UPDATE_CURRENT // se reemplazo 0 por max_id
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
if(Build.VERSION.SDK_INT < 23){
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
}
else{
alarmManager.setAlarmClock(new AlarmManager.AlarmClockInfo(cal.getTimeInMillis(), pendingIntent), pendingIntent);
}
masXdias=0;
cont1++;
}
else{
masXdias=masXdias+1;
}
}
}
private Notification getNotification(String content) {
Notification.Builder builder = new Notification.Builder(this);
builder.setContentTitle("Scheduled Notification");
builder.setContentText(content);
builder.setSmallIcon(getNotificationIcon());
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.movando2_xx));
builder.setAutoCancel(true);
return builder.build();
}
private int getNotificationIcon() {
boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
return useWhiteIcon ? R.drawable.movando2_xxbl : R.drawable.movando2_xx;
}
}
There are other java modules in my application, but the bug is in somewhere place of these code.
Thank you very much for your help, any hint and feedback is appreciated!.
Edwin

BoradcastReceiver Notificfication link to my app

I'm trying to create a Service that checks every x minutes for new messages on a server.
I was quit happy to found this post : Alarm Manager Example
MainActivity
package com.example.alarmexample;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startAlert();
} public void startAlert() {
int timeInSec = 2;
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (timeInSec * 1000), pendingIntent);
Toast.makeText(this, "Alarm set to after " + i + " seconds",Toast.LENGTH_LONG).show();
}
}
MyBroadcastReceiver.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.widget.Toast;
public class MyBroadcastReceiver extends BroadcastReceiver {
MediaPlayer mp;
#Override
public void onReceive(Context context, Intent intent) {
mp=MediaPlayer.create(context, R.raw.alarm);
mp.start();
Toast.makeText(context, "Alarm", Toast.LENGTH_LONG).show();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alarmexample" >
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.alarmexample.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="MyBroadcastReceiver" >
</receiver>
</application>
</manifest>
This does exact the thing I want. But if I try to use the Context from the BroadcastReceiver-class to notify the user. The App doesn't starts when the user clicks on the notification.
Is there something spezial I have to do to archive this ?
This is not a direct answer to your question (cant comment because not enough reputation) , but do you really need to check the server every x minutes for an event? I dont know anything about your project, but firebase https://firebase.google.com/docs/cloud-messaging/ would probably be a good alternative. It allows you to send Push Notifications to your client so that the client doesnt need to poll constantly.
Open application after clicking on Notification
Here is the answer - I was looking at the wrong end from this problem.

Difficulties on displaying another full screen activity for alarm clock app

I'm a student learning programming in Android Studio and I met some difficulties when developing my Alarm clock project. I've done lots of experiments and research in my code but still I couldn't find the solution. I wanted to try to make a normal alarm that is when the alarm is activated, display another full screen activity to remind the user. I don't want my alarm app to just ring and toast notification. I want it to also display a screen.
Here is my code:
package com.example.android.exno11;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.ToggleButton;
import com.example.android.exno11.AlarmReceiver;
import com.example.android.exno11.R;
import java.util.Calendar;
import static android.R.id.message;
import static android.provider.AlarmClock.EXTRA_MESSAGE;
public class MainActivity extends AppCompatActivity {
TimePicker alarmTimePicker;
PendingIntent pendingIntent;
AlarmManager alarmManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alarmTimePicker = (TimePicker) findViewById(R.id.timePicker);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
}
// Toggle button
public void OnToggleClicked(View view) {
long time;
if (((ToggleButton) view).isChecked()) {
Toast.makeText(MainActivity.this, "Activated", Toast.LENGTH_SHORT).show();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute() + 1);
Intent intent = new Intent(this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
time=(calendar.getTimeInMillis()-(calendar.getTimeInMillis()%60000));
if(System.currentTimeMillis()>time) {
if (calendar.AM_PM == 0)
time = time + (1000*60*60*12);
else
time = time + (1000*60*60*24);
}
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, 120000, pendingIntent);
}
else {
Toast.makeText(MainActivity.this, "OFF", Toast.LENGTH_SHORT).show();
alarmManager.cancel(pendingIntent);
}
}
}
And this is the manifest, not sure what I'm doing is right.Thank you for helping.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.exno11">
<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>
<receiver android:name=".AlarmReceiver"></receiver>
</application>
</manifest>

Alarm Receiver and Android Manifest

I've found several tutorials about setting the alarm receiver to send a toast message in set intervals. and i've been following the code and broken down my own project into 3 classes.
the HelloDroidActivity.java is:
package com.example.helloandroid;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.example.helloandroid.alarms.MyAlarmReciever;
public class HelloDroidActivity extends Activity {
/** Called when the activity is first created. */
public static int RTC_WAKEUP;
public static long INTERVAL_FIFTEEN_MINUTES;
private AlarmManager alarmMgr;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Krishneel");
setContentView(tv);
Toast.makeText(this, "Alarm went off", Toast.LENGTH_SHORT).show();
Log.d("OnCreate", "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcd");
alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, MyAlarmReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 5);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 7000, pendingIntent);
}
}
also the MyAlarmReciever.java(i am already aware of the spelling mistake on the name):
package com.example.helloandroid.alarms;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class MyAlarmReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.e("onReceive", "ladskjflsakjdflskjdflskjdfslkjdflasdf");
Toast.makeText(context, "OnReceive alarm test", Toast.LENGTH_SHORT).show();
}
}
and the Android Manifest which looks like this :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloandroid"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="com.example.helloandroid.HelloDroidActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="AlarmReceiver">
<intent-filter>
<action android:name="com.example.helloandroid.alarms" />
</intent-filter>
</receiver>
</application>
</manifest>
I have read that in order to get the project to receive my alarmReceiver java class I need to edit the manifest with a new receiver. but I'm fairly new to XML and dont know which direction to take.
There is already a receiver that you have defined in your manifest file. But the name is not correct see the name needs to be the full class name i.e the package.RecieverName. And in your case the name of your receiver is MyAlarmReciever. So the receiver will be defined as follows
<receiver android:name=".alarms.MyAlarmReciever">
<intent-filter>
<action android:name="com.example.helloandroid.alarms" />
</intent-filter>
</receiver>
In your manifest, the receiver is listening to an action called com.example.helloandroid.alarms. But in your HelloDroidActivity.java there is not such action is added to the intent.
public class HelloDroidActivity extends Activity {
//....
#Override
public void onCreate(Bundle savedInstanceState) {
//....
Intent intent = new Intent(this, MyAlarmReciever.class);
intent.setAction("com.example.helloandroid.alarms");
//....
}
}

Categories