How do I start a fragment in my Android application from a notification in the notification bar?
I've tried to implement this answer of creating my own action and then setting the action to the intent, but I'm unsure how to use it and what is required additionally - like adding something to the Manifest.
I've got a notification class that receives a context, a message and then an action. I then want to filter on that action to determine which fragment to launch, but I don't know how to launch a fragment as opposed to launching an activity.
Here is my Notifications.java class (incomplete):
public class Notifications {
private Context mContext;
public Notifications(Context context) {
this.mContext = context;
}
public static void notify(Context context, String message, String action) {
//Action you invent should include the application package as a prefix — for example: "com.example.project.SHOW_COLOR".
action = "my.package.name.here.frag."+action;
//Construct a user message.
String appName = context.getResources().getString(R.string.app_name);
// Use the Notification manager to send notification
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Create a notification using android stat_notify_chat icon.
Notification notification = new Notification(R.drawable.ic_stat_notification, message, 0);
//Sound, lights, vibration.
//REMEMBER PERMISSIONS.
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
// Create a pending intent to open the application when the notification is clicked.
//Restart the app.
Intent launchIntent = null;
//Get the action and based on what the action is, launch the application displaying the appropriate fragment.
if (action.equalsIgnoreCase("friend")){
//New friend notification
//Launch application displaying the list of friends
}
if (action.equalsIgnoreCase("article")){
//New article has been posted
//Launch application displaying the news feed fragment
}
if (action.equalsIgnoreCase("points")){
//Points scored notification
//Launch application displaying the user's profile
}
if (action.equalsIgnoreCase("redeemable")){
//New redeemable is offered
//Launch application displaying the list of redeemables
}
if (!action.equalsIgnoreCase("friend")
&& !action.equalsIgnoreCase("article")
&& !action.equalsIgnoreCase("points")
&& !action.equalsIgnoreCase("redeemable")){
//Not specific, so launch the application from scratch displaying the activity feed
launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
}
if(action != null && launchIntent != null){
launchIntent.setAction(action);
}
// Set the notification and register the pending intent to it
notification.setLatestEventInfo(context, appName, message, pendingIntent);
// Trigger the notification
notificationManager.notify(0, notification);
}
}
So this was actually pretty easy. Hopefully I can help someone else see this too.
I send an action to this notify function. The I add that action to my intent to launch an activity. In my case I open the launching activity, because all the fragments are loaded from within that activity based on what the user does. So I set the action using setAction and the I use that action in the activity as below.
My Notifications.java class changed to this:
public static void notify(Context context, String message, String action) {
action = action.toUpperCase();
// Create a pending intent to open the the application when the notification is clicked.
//Restart the app.
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
if(action != null && launchIntent != null){
launchIntent.setAction(action);
}
PendingIntent pendingIntent = PendingIntent.getActivity(context, -1, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.when = System.currentTimeMillis();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Set the notification and register the pending intent to it
notification.setLatestEventInfo(context, appName, message, pendingIntent);
// Trigger the notification
notificationManager.notify(0, notification);
}
And then in my activity from where I load the fragments, I get the action and filter it:
Intent intent = getIntent();
try{
String action = intent.getAction().toUpperCase();
if(action != null){
if(action.equalsIgnoreCase(getResources().getString(R.string.notification_action_friend))){
goFrag(getResources().getInteger(R.integer.FRAG_A_INT));
}
if(action.equalsIgnoreCase(getResources().getString(R.string.notification_action_article))){
goFrag(getResources().getInteger(R.integer.FRAG_B_INT));
}
if(action.equalsIgnoreCase(getResources().getString(R.string.notification_action_points))){
goFrag(getResources().getInteger(R.integer.FRAG_C_INT));
}
if(action.equalsIgnoreCase(getResources().getString(R.string.notification_action_redeemable))){
goFrag(getResources().getInteger(R.integer.FRAG_D_INT));
}
if(action.equalsIgnoreCase(getResources().getString(R.string.notification_action_dance))){
goFrag(getResources().getInteger(R.integer.FRAG_E_INT));
}
}else{
Log.d(TAG, "Intent was null");
}
}catch(Exception e){
Log.e(TAG, "Problem consuming action from intent", e);
}
In my goFrag function I replace the fragment if the required fragment is still in memory (meaning the user was there earlier and it hasn't been destroyed yet), or I create a new fragment required.
Related
I have an app with contextual commands. After triggered a contextual command, it will make a HTTP request with a link and post the result on the card, something like, "Completed!". I want this card to be closed by itself after one second so that the user need not to tap to close it. Once the result card is closed, it will go back to contextual command lists with "Ok, glass" at footer and ready for next command.
May i know how to do that?
private class HTTPRequest extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... arg0) {
try {
if (mWhat.equalsIgnoreCase("GET")) {
// get json via YouTube API
URL url = new URL("http://example.com");
mUrlConnection = (HttpURLConnection)
url.openConnection();
InputStream in = new BufferedInputStream(
mUrlConnection.getInputStream());
int ch;
StringBuffer b = new StringBuffer();
while ((ch = in.read()) != -1) {
b.append((char) ch);
}
mResult = new String(b);
}
} catch (Exception e) {}
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
mTvInfo.setText(mResult);
}
You can use an Android Dialog for this:
Use CardBuilder to create the "Completed" card using the MENU layout.
Create a new instance of Dialog and set its content view to be the view returned by CardBuilder.getView.
Show the dialog.
Use Handler.postDelayed (or some similar mechanism) to automatically dismiss the dialog after the desired amount of time has passed.
I am attempting to create a C++ bridge for the IOS facebook SDK. I can log in fine, but when I try to retrieve the user details the graph functions never seem to execute though I can step over them in the debugger. Here's what I have:
void facebook_bridge::performLoginWithFacebook()
{
// Open a session showing the user the login UI
// You must ALWAYS ask for basic_info permissions when opening a session
[FBSession openActiveSessionWithReadPermissions:#[#"basic_info", #"email"]
allowLoginUI:YES
completionHandler:
^(FBSession *session, FBSessionState state, NSError *error) {
// Retrieve the app delegate
AppController* appDelegate = (AppController*)[UIApplication sharedApplication].delegate;
// Call the app delegate's sessionStateChanged:state:error method to handle session state changes
[appDelegate sessionStateChanged:session state:state error:error];
}];
isLoggedIn = true;
}
cocos2d::CCDictionary* facebook_bridge::getUserDetails()
{
__block NSDictionary *currentPermissions;
// Request the permissions the user currently has
[FBRequestConnection startWithGraphPath:#"/me/permissions"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error){
// These are the current permissions the user has
currentPermissions= [(NSArray *)[result data] objectAtIndex:0];
NSLog(#"No error");
// We will store here the missing permissions that we will have to request
// If we have permissions to request
} else {
NSLog([NSString stringWithFormat:#"error %#", error.description]);
}
}];
cocos2d::CCDictionary* dict = cocos2d::CCDictionary::create();
for(int i = 0; i < currentPermissions.count; i++) //get a bad access here, currentPermissions is always null
{
NSString* val = currentPermissions.allValues[i];
NSString* k = currentPermissions.allKeys[i];
std::string value = *new std::string(val.UTF8String);
std::string key = *new std::string(k.UTF8String);
cocos2d::CCString *ccVal = cocos2d::CCStringMake(value);
dict->setObject((cocos2d::CCObject*)ccVal, key);
}
dict->retain();
return dict;
}
I think the FBRequest is asynchronous, so I tried placing it in the first function to run on the same thread, to no avail.
Any help? (I'm very new to objective C, but have experience in most other languages)
I think the FBRequest is asynchronous, so I tried placing it in the
first function to run on the same thread, to no avail.
I think you are completely missing the meaning of "asynchronous". You don't have to have threads for things to be asynchronous -- you can execute something asynchronously on the same thread. (Though in this case it probably does its main stuff on another thread, and then runs the completion block on the original thread.)
When you call an asynchronous API, the completion block will not execute before the rest of your function. Pretty much guaranteed.
I am using the following code to send an SMS from my app;
void App::sendSms(const QString &messageText, const QStringList &phoneNumbers) {
bb::pim::account::AccountService accountService;
bb::pim::message::MessageService messageService;
QList<Account> accountListy = accountService.accounts(bb::pim::account::Service::Messages,"sms-mms");
bb::pim::account::AccountKey smsAccountId = 0;
if(!accountListy.isEmpty()) {
smsAccountId = accountListy.first().id();
qDebug() << "SMS-MMS account ID:" << smsAccountId;
}
else {
qWarning() << "Could not find SMS account";
return;
}
QList<bb::pim::message::MessageContact> participants;
foreach(const QString &phoneNumber, phoneNumbers) {
bb::pim::message::MessageContact recipient = bb::pim::message::MessageContact(
-1, bb::pim::message::MessageContact::To,
phoneNumber, phoneNumber);
participants.append(recipient);
}
bb::pim::message::ConversationBuilder *conversationBuilder =
bb::pim::message::ConversationBuilder::create();
conversationBuilder->accountId(smsAccountId);
conversationBuilder->participants(participants);
bb::pim::message::Conversation conversation = *conversationBuilder;
bb::pim::message::ConversationKey conversationId = messageService.save(smsAccountId, conversation);
bb::pim::message::MessageBuilder *builder =
bb::pim::message::MessageBuilder::create(smsAccountId);
builder->conversationId(conversationId);
builder->addAttachment(bb::pim::message::Attachment("text/plain", "", messageText.toUtf8()));
foreach(const bb::pim::message::MessageContact recipient, participants) {
builder->addRecipient(recipient);
}
bb::pim::message::Message message = *builder;
messageService.send(smsAccountId, message);
delete builder;
delete conversationBuilder;
}
However everytime it sends a new SMS, it creates a new thread in the Text Messages UI. I was wondering if there was a way to add the new message to the thread that already exists for the number it is going to send to?
Thanks!
The aspect of your code which causes this bug is
// at top of file
using namespace bb::pim::messages;
ConversationBuilder *conversationBuilder = ConversationBuilder::create();
conversationBuilder->accountId(smsAccountId);
conversationBuilder->participants(participants);
Conversation conversation = *conversationBuilder;
ConversationKey conversationId = messageService.save(smsAccountId, conversation);
This piece—following its preceeding lines, would create a new conversation for the participants regardless of previous existing conversations in the Hub.
To work around this, the BlackBerry Cascades PIM MessageService provides a MessageSearchFilter with which you can use to filter the conversations by any SearchFilterCriteria. Use it thus…
//Setup a filter
MessageFilter filter;
//Set our filter to filter conversations with id of the contact
filter.insert(MessageFilter::ContactId, contactId);
//Run filter
filterdConvosKeys = messageService.conversationKeys(smsAccountId, filter);
ConversationKey conversation_id;
//Vars for conversation builder
conversationBuilder->accountId(smsAccountId);
conversationBuilder->participants(participants);
//Get conversation ID for existing conversation, else create new
if (filterdConvosKeys.count() > 1) {
// go through all conversations for this contact and choose
// the conversation in which this contact is the sole participant
else if (filterdConvosKeys.count() == 1) {
conversation_id = filterdConvosKeys[0].toAscii();
} else {
conversation_id = messageService.save(smsAccountId, conversation);
}
Edit
Despite what the original Source says, I find it a bit buggy. If you use it exactly as it says, you will always end up with a new conversation if there is no conversation with the contact as the only participant. I tried doing a search in the BlackBerry Hub on my STL100-3 with the phone number of a contact, I ended up with many messages which were in the same conversation. But this means there is a possibility that many conversations are being returned if you filter by MessageFilter::Participants. It is better to filter using MessageFilter::ContactId.
p.s: I namespaced the code blocks so bb::pim::messages:: is not repeated.
I am executing some commands on linux using JSCH and taking output of that command and printing same on the activity log in my JAVAFX application.
Here is a problem: the activity log area is getting stuck after printing some line. But, if I switch from the window and back application continues to print lines on log area. I have debugged several times but wasn't able to catch the problem.
Below is the code
channel.connect();
PrintStream commander = new PrintStream(channel.getOutputStream(), true);
commander.println(command_cd);
commander.println(" exit;");
commander.close();
InputStream outputstream_from_the_channel = channel.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(outputstream_from_the_channel));
String jarOutput;
BufferedWriter bw = new BufferedWriter(new FileWriter(resultLogFile.getAbsolutePath(), true));
while ((jarOutput = reader.readLine()) != null) {
this.logger.info("Status Update = " + jarOutput);
System.out.print("Status Update on screen ="+jarOutput + "\n");
bw.write(jarOutput);
bw.newLine();
outputFromUnix.append(jarOutput).append("\n");
// Display in activity log area in realtime.
if (DeploymentTaskController.actLogTArea != null) {
System.out.println("outputFromUnix.toString()---->>>>> " + outputFromUnix.toString());
//actlogArea is TextArea
DeploymentTaskController.actLogTArea.setText(outputFromUnix.toString());
DeploymentTaskController.actLogTArea.end();
}
}
bw.close();
reader.close();
JavaFX has a special thread named "JavaFX Application Thread" to update all UI. This thread shouldn't be used for any non-UI operations, and all UI operations must be run on that thread.
Make sure you run your code in a separate Thread and wrap all UI code into Platform#runLater() to have executed
// Using separate thread to run data loading code
new Thread(new Runnable() {
public void run() {
channel.connect();
// [...] all your channel reading code
// Updating UI from JavaFX Application Thread
if (DeploymentTaskController.actLogTArea != null) {
System.out.println("outputFromUnix.toString()---->>>>> " + outputFromUnix.toString());
final String outputStr = outputFromUnix.toString();
Platform.runLater(new Runnable() {
#Override
public void run() {
DeploymentTaskController.actLogTArea.setText(outputStr);
DeploymentTaskController.actLogTArea.end();
}
});
}
bw.close();
reader.close();
}
}).start();
There is also a special concurrency API in JavaFX for such tasks, see here: http://docs.oracle.com/javafx/2/threads/jfxpub-threads.htm
I'm trying to write some code that discards all keyboard and mouse events when enabled on Mac OSX 10.6. My code runs as the root user. The approach I'm taking is to create an event tap that discards all events passed to it (while enabled). The event tap callback function looks like this:
CGEventRef MyTapCallback(CGEventTapProxy proxy,
CGEventType type,
CGEventRef event,
void *refcon)
{
return CKeyLocker::isEnabled() ? NULL : event;
}
And the code I'm using to enable and disable the event tap looks like this:
void CKeyLocker::enable(bool bEnable)
{
if (bEnable == m_bEnabled)
return;
if (bEnable)
{
// which events are we interested in?
CGEventMask evMask = kCGEventMaskForAllEvents;
CFMachPortRef mp = CGEventTapCreate(kCGHIDEventTap,
kCGHeadInsertEventTap,
kCGEventTapOptionDefault,
evMask,
MyTapCallback,
NULL);
if (mp)
{
qDebug() << "Tap created and active. mp =" << mp;
m_enabledTap = mp;
m_bEnabled = true;
}
}
else
{
CGEventTapEnable(m_enabledTap, false);
CFRelease(m_enabledTap);
m_enabledTap =0;
m_bEnabled = false;
qDebug() << "Tap destroyed and inactive";
}
}
This approach works very well while the event tap is active - I can hammer on the keyboard and mouse as much as I want and no events make it through the system. However, when the tap is disabled all the keys I pushed while the tap was active appear in the current window. It's like the event tap is just delaying the events, rather than destroying them, which is odd, since the Mac documentation clearly states:
If the event tap is an active filter, your callback function should return one of the following:
The (possibly modified) event that is passed in. This event is passed back to the event system.
A newly-constructed event. After the new event has been passed back to the event system, the new event will be released along with the original event.
NULL if the event passed in is to be deleted.
I'm returning NULL, but the event doesn't seem to be deleted. Any ideas?
The linked comment does not have an answer from what I see, so I'll dump some info from what I've seen when poking around with this stuff.
First, I have much better luck with CGEventTapCreateForPSN. It's as if the system gives you some leeway for restricting your tap. However, from this example it looks like this is not sufficient.
Next up - and this /may/ be all you need... In your call back, you probably want (and may need) to check for the following events:
switch (type)
{
case kCGEventTapDisabledByTimeout:
case kCGEventTapDisabledByUserInput:
{
CFMachPortRef *pTap = (CFMachPortRef*)refcon;
CGEventTapEnable( *pTap, true );
return NULL;
}
default:
break;
}
Regardless of what the various documentation does or doesn't say, it's been my observation that the OS feels like it's 'probing' for bad callbacks; basically disabling event tap callbacks that are universally eating events. If you re-register in these cases the OS seems to be ok with it, as if saying: OK, you seem to know what you're doing, but I'll probably poke you again in a bit to make sure.
It's really strange, we use event taps for the same purpose (input blocking in a given scenario) and works perfectly 10.4 - 10.8.2. excpet one thing, it should not block or receive events from a password dialog (which is not a big surprise)
What I can see now is different compared to you sample is:
we use kCGTailAppendEventTap instead of kCGHeadInsertEventTap (this should not matter)
we do some event logging in the installed callback
we have some user event data in some self injected events, that filtered out, but apart from this we simply return NULL to drop an unwanted event (like you do), I can confirm, not all events are ignorable!
we turn on/off the event tap this way:
bool SetInputFilter(bool bOn)
{
bool result = false;
CFRunLoopRef runLoopRef = CFRunLoopGetMain();
if (bOn) {
// Create an event tap.
CGEventMask eventMask = kCGEventMaskForAllEvents;
if ((m_eventTapInput = CGEventTapCreate(kCGHIDEventTap,
kCGTailAppendEventTap,
kCGEventTapOptionDefault,
eventMask, CGInputEventCallback, this)) == NULL) {
Log(L"Failed to create event tap");
return result;
}
// Create a run loop source.
m_runLoopEventTapSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, m_eventTapInput, 0);
CFRelease(m_eventTapInput); // CFMachPortCreateRunLoopSource retains m_eventTapInput
if (m_runLoopEventTapSource == NULL) {
Log(L"Failed to create run loop source for event tap");
return result;
}
// Add to the current run loop.
CFRunLoopAddSource(runLoopRef, m_runLoopEventTapSource, kCFRunLoopCommonModes);//kCFRunLoopDefaultMode);
CFRelease(m_runLoopEventTapSource); // CFRunLoopAddSource retains m_runLoopEventTapSource
result = true;
}
else {
// Disable the event tap.
if (m_eventTapInput)
CGEventTapEnable(m_eventTapInput, false);
// Remove our run loop source from the current run loop.
if (runLoopRef && m_runLoopEventTapSource) {
CFRunLoopRemoveSource(runLoopRef, m_runLoopEventTapSource, kCFRunLoopCommonModes);//kCFRunLoopDefaultMode);
m_runLoopEventTapSource = NULL; // removing m_runLoopEventTapSource releases last reference of m_runLoopEventTapSource too
m_eventTapInput = NULL; // removing m_runLoopEventTapSource releases last reference of m_eventTapInput too
}
}
return result;
}
I can verify that returning NULL does effectively delete some events, but i have also seen times when it does not, exactly how it decides what deletions to permit is unclear but it looks like mass deletions seem to be prevented e.g.: when you delete more than 100 events or so in a row.