Lost line in crash manager

I have a problem with a text string attached to an alarm. It seems that when the alarm goes off, the line associated with the alarm is zero. Itโ€™s clear that I made a mistake, but I donโ€™t know where.

My code for setting the alarm:

static void set_alarm(long alarm_time_in_millis,Context cont,AlarmManager alarm_manager,String str) { Intent intent = new Intent(cont, to_call_when_alarm_goes_off.class); intent.putExtra("string_passed_in_bundle", str); Log.i("xx","set_alarm ["+str+"]"); // The string I see in the Log is correct PendingIntent pIntent = PendingIntent.getBroadcast(cont,0, intent, 0); alarm_manager.cancel(pIntent); alarm_manager.set(AlarmManager.RTC_WAKEUP,alarm_time_in_millis, pIntent); } 

The code for receiving the alarm is as follows:

 public class to_call_when_alarm_goes_off extends BroadcastReceiver { Bundle bundle_from_whoever_called_this_activity; @Override public void onReceive(Context arg0, Intent arg1) { String str; bundle_from_whoever_called_this_activity = arg1.getExtras(); str = bundle_from_whoever_called_this_activity.getString("string_passed_in_bundle"); Log.i("xx","to_call_when_alarm_goes_off: TIME TO WAKE UP!!! ["+str+"]"); try { Intent i = new Intent(arg0, dingaling.class); i.putExtra("string_passed_in_bundle", str); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); arg0.startActivity(i); } catch (Exception e) { } } } 

When I set the alarm and wait for it to start, the log file will show something like this

 set_alarm [Go to meeting] to_call_when_alarm_goes_off: TIME TO WAKE UP!!! [null] 

EDIT: Maybe some kind of "super." feature I forgot to call?

EDIT: I often get confused about which context is passed to various functions, for example. getBaseContext (), getApplicationContext (), "this", etc. If I had the wrong context linked somewhere, could this cause this problem?

+4
source share
5 answers

I just copy and paste the code into my application and it works great. Just create in action and copy your broadcast receiver. Take one button in action, and onCLick buttons call your function for setAlarm.

I can get the string in broadcastReceiver.

Here is my code.

  public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.button1).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); set_alarm(System.currentTimeMillis(), MainActivity.this, manager, "Time for tea"); } }); } static void set_alarm(long alarm_time_in_millis,Context cont,AlarmManager alarm_manager,String str) { Intent intent = new Intent(cont, to_call_when_alarm_goes_off.class); intent.putExtra("string_passed_in_bundle", str); Log.i("xx","set_alarm ["+str+"]"); // The string I see in the Log is correct PendingIntent pIntent = PendingIntent.getBroadcast(cont,0, intent, 0); alarm_manager.cancel(pIntent); alarm_manager.set(AlarmManager.RTC_WAKEUP,alarm_time_in_millis, pIntent); } } 

The broadcast receiver is the same as yours, so I do not put it here.

Finally declare the broadcast receiver in the manifest

Output

 set_alarm [Time for tea] to_call_when_alarm_goes_off: TIME TO WAKE UP!!! [Time for tea] 
+2
source

You can go through this code and make sure that the string you get is what you expect. I bet you are not getting what you expect.

+1
source

Use Intent instead of Bundle , so ...

 str = arg1.getStringExtra("string"); 
+1
source

When you change the key from

 "string" 

to

 "alarm_string" 

Are you still getting a string in the to_call_when_alarm_goes_off class? Or do you get zero?

What happens if you remove the undo function (pIntent) here:

 alarm_manager.cancel(pIntent); alarm_manager.set(AlarmManager.RTC_WAKEUP,alarm_time_in_millis, pIntent); 

Just trying to reduce the noise. "string" can be used in serialization or something like that, it can get confused. A more unique meaning also helps.

+1
source

The following code worked for me. I call BroadcastReceiver using a special intent with an intent filter in AndroidManifest. However, I cannot say why this would make a difference. In doing so, the code below works.

 public class MainActivity extends Activity implements OnClickListener { private Button button; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button1); button.setOnClickListener(this); } public void onClick(View v) { switch (v.getId()) { case R.id.button1: setAlarm(); break; } } private void setAlarm() { AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); long interval = System.currentTimeMillis() + 10000; // 10 seconds from now Intent launchIntent = new Intent(); launchIntent.putExtra("your_key", "your string value"); launchIntent.setAction(MyBroadcastReceiver.CUSTOM_INTENT); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, launchIntent, 0); alarmManager.set(AlarmManager.RTC_WAKEUP,interval, pendingIntent); } } 

And BroadcastReceiver:

 public class MyBroadcastReceiver extends BroadcastReceiver { public static final String CUSTOM_INTENT = "com.your_custom_intent"; @Override public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); String message = bundle.getString("your_key"); Log.d("test", message); } } 

And AndroidManifest:

 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.test2_2" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".MyBroadcastReceiver"> <intent-filter> <action android:name="com.your_custom_intent" /> </intent-filter> </receiver> </application> </manifest> 
+1
source

All Articles