I thought that Action was preeminent during intent resolution and if
the intent had the same action as the intent filter, it would be
considered a match regardless of data, if the filter did not specify
anything.
So I was very surprised to find that supplying action+data where
filter had only action, causes it to fail.
In the code below, if you comment out line 47 so that data is *not*
sent, only then it resolves correctly.
alertIntent.setData(alert_i);
To get it to work with the data (an id that is simply a String), what
mime type should I specify in the intent filter?
thanks...
-----
package com.iovercomer;
import java.util.Calendar;
import java.util.Date;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentFilter.MalformedMimeTypeException;
import android.net.Uri;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class TestIntentResolution extends Activity {
public static final String ALERT_ME_ACTION =
"com.iovercomer.TestIntentResolution.alertMe";
AlarmManager am;
PendingIntent mAlarmSender;
String uriBase = "content://com.iovercomer/alerts/";
Intent alertIntent;
int alarmNumber = 99;
protected int requestCode = 23;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button startButton = (Button) findViewById(R.id.start_alarm);
startButton.setOnClickListener(mStartAlarmListener);
am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alertIntent = new Intent(ALERT_ME_ACTION);
}
private OnClickListener mStartAlarmListener = new OnClickListener() {
@Override
public void onClick(View v) {
Uri alert_i = Uri.parse(uriBase + alarmNumber);
// comment out the following line to get the intent to
resolve to
the receiver
alertIntent.setData(alert_i);
int flags = PendingIntent.FLAG_UPDATE_CURRENT;
mAlarmSender =
PendingIntent.getBroadcast(TestIntentResolution.this,
requestCode , alertIntent, flags);
long firstTime = SystemClock.elapsedRealtime();
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime
+ 10 *
1000, mAlarmSender);
Toast.makeText(TestIntentResolution.this,
"starting >>>" + "alarm " +
alarmNumber,
Toast.LENGTH_LONG).show();
}
};
@Override
public void onResume() {
super.onResume();
Intent inte = registerReceiver(receiver, new IntentFilter(
ALERT_ME_ACTION));
}
@Override
public void onPause() {
super.onPause();
unregisterReceiver(receiver);
}
void alertMe(String time, String data) {
String t = "alert! at " + time + " " + data + "\n";
Toast.makeText(getApplicationContext(), t,
Toast.LENGTH_SHORT).show();
TextView tv = (TextView) findViewById(R.id.myginew);
tv.setText(t);
}
MyAlarmReceiver receiver = new MyAlarmReceiver();
public class MyAlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Date dt = Calendar.getInstance().getTime();
intent.getAction();
intent.getType();
Uri d = intent.getData();
String frag = (d == null ? "":d.getLastPathSegment());
alertMe(dt.toString(), frag);
}
};
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en