Android alarm receiver and manifest

I found several guides for setting up an alarm receiver to send toast at set intervals. and I follow the code and split my own project into 3 classes.

HelloDroidActivity.java:

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 MyAlarmReciever.java (I already know about spelling in a 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 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 read that in order for the project to receive my java-class alarmReceiver, I need to edit the manifest with a new receiver. but I'm pretty new to XML and don't know which direction to go.

+6
source share
2 answers

Your manifest file already has a recipient. But this name is incorrect, since the name must be the full name of the class ie package.RecieverName. And in your case, the name of your receiver is MyAlarmReciever. Thus, the receiver will be defined as follows

 <receiver android:name=".alarms.MyAlarmReciever"> <intent-filter> <action android:name="com.example.helloandroid.alarms" /> </intent-filter> </receiver> 
+3
source

In your manifest, the listener listens for an action called com.example.helloandroid.alarms . But in your HelloDroidActivity.java , such an action is not 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"); //.... } } 
0
source

Source: https://habr.com/ru/post/914851/


All Articles