从后台运行应用程序并抛出状态通知

时间:2012-07-04 13:30:55

标签: android air

我正在使用带有本机扩展的Adobe AIR 3.2为Android操作sctipt编写应用程序。我的任务是发出通知抛出本机扩展,以便点击该通知,它必须从后台打开应用程序。我已经开始运行通知,但是当我点击它时我无法运行应用程序。我怎么能这样做?我做错了什么?请帮帮我,谢谢。

package com.company.extensions;

import java.util.HashMap;
import java.util.Map;

  import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;
import android.util.Log;

import com.adobe.fre.FREContext;
import com.adobe.fre.FREFunction;

public class ToolsExtensionContext extends FREContext 
{
    public Vibrator vb = null;

    @Override
    public void dispose() 
    {

    }

    @Override
    public Map<String, FREFunction> getFunctions() 
    {
        Map<String, FREFunction> functionMap = new HashMap<String, FREFunction>();

        functionMap.put("notifyUser", new ToolsNotifyFunction());

        return functionMap;
    }

    void notifyUser()
    {
        Log.d("bla-bla-bla", "Just bla");


        // look up the notification manager service
        NotificationManager nm = (NotificationManager)this.getActivity().getSystemService(Context.NOTIFICATION_SERVICE);

        // The details of our fake message
        CharSequence from = "Hello";
        CharSequence message = "Some message!";

        // The PendingIntent to launch our activity if the user selects this
        // notification.  Note the use of FLAG_CANCEL_CURRENT so that, if there
        // is already an active matching pending intent, cancel it and replace
        // it with the new array of Intents.
        Intent k = new Intent(this.getActivity().getApplicationContext(), ToolsExtensionContext.class);

        PendingIntent contentIntent = PendingIntent.getService(this.getActivity().getApplicationContext(), 
                0, k , PendingIntent.FLAG_CANCEL_CURRENT);

        // The ticker text, this uses a formatted string so our message could be localized
        String tickerText = "Tiker text";

        // construct the Notification object.
        Notification notif = new Notification(R.drawable.close, tickerText, System.currentTimeMillis() + 100000);

        // Set the info for the views that show in the notification panel.
        notif.setLatestEventInfo(this.getActivity().getApplicationContext(), from, message, null);//contentIntent);

        // We'll have this notification do the default sound, vibration, and led.
        // Note that if you want any of these behaviors, you should always have
        // a preference for the user to turn them off.
        notif.defaults = Notification.DEFAULT_ALL;
        notif.flags = Notification.FLAG_AUTO_CANCEL;

        // Note that we use R.layout.incoming_message_panel as the ID for
        // the notification.  It could be any integer you want, but we use
        // the convention of using a resource id for a string related to
        // the notification.  It will always be a unique number within your
        // application.
        nm.notify(1, notif);
    }
}

1 个答案:

答案 0 :(得分:1)

以下是在Android上发送本地通知的工作代码,并且在通知恢复到同一活动的应用程序后,在进入后台之前:

void sendLocalNotification()
    {

        // look up the notification manager service
        NotificationManager nm = (NotificationManager)this.getActivity().getSystemService(Context.NOTIFICATION_SERVICE);

        // The details of our fake message
        CharSequence from = "Hello";
        CharSequence message = "some message";

        // The PendingIntent to launch our activity if the user selects this
        // notification.  Note the use of FLAG_CANCEL_CURRENT so that, if there
        // is already an active matching pending intent, cancel it and replace
        // it with the new array of Intents.


        Intent k = new Intent(this.getActivity().getApplicationContext(), this.getActivity().getClass());
        Log.d("bla-bla-bla",  this.getActivity().getClass().toString());
        k.setAction(Intent.ACTION_MAIN);
        k.addCategory(Intent.CATEGORY_LAUNCHER);


        PendingIntent contentIntent = PendingIntent.getActivity(this.getActivity().getApplicationContext(), 
                0, k , PendingIntent.FLAG_CANCEL_CURRENT);

        //PendingIntent contentIntent = PendingIntent.getService(this.getActivity().getApplicationContext(), 
        //      0, k , PendingIntent.FLAG_CANCEL_CURRENT);

        // The ticker text, this uses a formatted string so our message could be localized
        String tickerText = "Tiker text";

        // construct the Notification object.
        Notification notif = new Notification(R.drawable.close, tickerText, System.currentTimeMillis() + 100000);

        // Set the info for the views that show in the notification panel.
        notif.setLatestEventInfo(this.getActivity().getApplicationContext(), from, message, contentIntent);

        // We'll have this notification do the default sound, vibration, and led.
        // Note that if you want any of these behaviors, you should always have
        // a preference for the user to turn them off.
        notif.defaults = Notification.DEFAULT_ALL;
        notif.flags = Notification.FLAG_AUTO_CANCEL;

        // Note that we use R.layout.incoming_message_panel as the ID for
        // the notification.  It could be any integer you want, but we use
        // the convention of using a resource id for a string related to
        // the notification.  It will always be a unique number within your
        // application.
        nm.notify(1, notif);
    }