在Blackberry通知栏中创建定期通知

时间:2011-10-26 10:33:34

标签: blackberry notifications

我已经阅读了一些关于Blackberry推送通知的内容,但我不确定提供的代码是否是我想要实现的。基本上,我希望我的应用程序在一段时间后弹出通知,带有图标和消息,一旦他们点击该通知,它就会将用户带到相关的类,并删除通知。我可以很容易地在Android中做到这一点但是我没有找到我想要的黑莓手机。我已经实现了我的计时器代码,所以没问题,但我希望有人指点我的教程或正确的方向。通知应在通知栏(如下所示)和通知屏幕(也如下所示)中弹出。

我尝试过这样的代码,但没有成功:

ApplicationIndicatorRegistry reg = ApplicationIndicatorRegistry.getInstance();    

                                    EncodedImage image = EncodedImage.getEncodedImageResource("logosmall.png");

                                    ApplicationIcon icon = new ApplicationIcon( image );

                                    ApplicationIndicator indicator = reg.register( icon, false, true);
                                    indicator.setIcon(icon);
                                    indicator.setVisible(true);

enter image description here

enter image description here

谢谢, 插口 我现在也试过这个:

public ApplicationIndicator _indicator; 
public static handleGPSListener _instance;

public void setupIndicator() {

    //Setup notification 
    if (_indicator == null) {
        ApplicationIndicatorRegistry reg = ApplicationIndicatorRegistry.getInstance();
        _indicator = reg.getApplicationIndicator();

        if(_indicator == null) {
            ApplicationIcon icon = new ApplicationIcon(EncodedImage.getEncodedImageResource ("daslogo.png"));
            _indicator = reg.register(icon, false, true);  
            _indicator.setValue(0);
            _indicator.setVisible(false);
        }
    }

}

public void setVisible(boolean visible, int count) {

    if (_indicator != null) {
        if (visible) {
            _indicator.setVisible(true);
            _indicator.setValue(count);
        } else {
            _indicator.setVisible(false);
        }
    }

}

handleGPSListener () {

}

public static handleGPSListener getInstance() {
    if (_instance == null) {
        _instance = new handleGPSListener ();
    }
    return(_instance);
}

我从下面的另一个void方法调用setupIndicator方法:

public void LocationUpdate(){
setupIndicator();
}

如果它也有帮助,这就是全班:

package mypackage;

import javax.microedition.location.Coordinates;
import javax.microedition.location.Location;
import javax.microedition.location.LocationException;
import javax.microedition.location.LocationListener;
import javax.microedition.location.LocationProvider;

import net.rim.blackberry.api.messagelist.ApplicationFolderIntegrationConfig;
import net.rim.blackberry.api.messagelist.ApplicationIcon;
import net.rim.blackberry.api.messagelist.ApplicationIndicator;
import net.rim.blackberry.api.messagelist.ApplicationIndicatorRegistry;
 import net.rim.blackberry.api.messagelist.ApplicationMessageFolder;
import net.rim.blackberry.api.messagelist.ApplicationMessageFolderRegistry;
import net.rim.device.api.database.Cursor;
import net.rim.device.api.database.Database;
import net.rim.device.api.database.DatabaseFactory;
import net.rim.device.api.database.Row;
import net.rim.device.api.database.Statement;
import net.rim.device.api.io.URI;
import net.rim.device.api.system.ApplicationDescriptor;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransport;

public class handleGPSListener implements LocationListener {
Coordinates c = null;
private double lat=0.00;
private double lon=0.00;
Database sqliteDB;
String username;
public static final String NAMESPACE = "http://tempuri.org/";
public String URL = "http://77.245.77.195:60010/Webservice/IDLMobile.asmx?WSDL";
public static final String SOAP_ACTION = "http://tempuri.org   /Get_OfferCount_By_Location";
public static final String METHOD_NAME = "Get_OfferCount_By_Location";
private double x,y;
public void locationUpdated(LocationProvider loc, Location location) {  //method to update as the location changes.
    System.out.println("class handle GPS Listener");
    if (loc == null) {  //condition to check if the location information is null.
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run() {
                Dialog.alert("GPS not supported!"); //dialog box to alert gps is not started.
                System.out.println("Problem 1");
                return;
            }
        });
    } else {    //if not checked.
        System.out.println("OK");
        switch (loc.getState()) {   //condition to check state of the location.
        case (LocationProvider.AVAILABLE):  //condition to check if the location is available.

            System.out.println("Provider is AVAILABLE");

            try {
                location = loc.getLocation(-1); //location to get according to user present.
            } catch (LocationException e) {
                return;

            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (location != null && location.isValid()) {   //condition to check if the location is not null and is valid.
                c = location.getQualifiedCoordinates(); //to get the coordinates of the location.
            }
            if (c != null) {    //condition to check if the location is not null.
                lat = c.getLatitude();  //retrieve the latitude values into variable.
                lon = c.getLongitude(); //retrieve the longitude values into variable.
                System.out.println("lat and lon"+lat+lon);
                UiApplication.getUiApplication().invokeLater(
                        new Runnable() {
                            public void run() {
                                updateFields();
                                getValues();
                                // Dialog.alert(lat+"GPS supported!"+lon);
                                return;
                            }

                            private void getValues() {
                                // TODO Auto-generated method stub
                                try {
                                    URI uri = URI
                                            .create("file:///SDCard/"
                                                    + "database3.db");  //database3 to retrieve the values from location table.
                                    sqliteDB = DatabaseFactory.open(uri);
                                    Statement st = null;
                                    st = sqliteDB
                                            .createStatement("SELECT Latitude,Longitude FROM Location");//statement to retrieve the lat and lon values.
                                    st.prepare();
                                    Cursor c = st.getCursor();//cursor to point.
                                    Row r;
                                    int i = 0;
                                    while (c.next()) {  //loop to execute until there are no values in the cursor.
                                        r = c.getRow(); //store the values in row.
                                        i++;
                                        lat=Double.parseDouble(r.getString(0)); //retrieve the latitude values from the database and store in variable.
                                        lon=Double.parseDouble(r.getString(1)); //retrieve the longitude values from the database and store in variable.
                                        System.out.println(r.getString(0)
                                                + " Latitude");
                                        System.out.println(r.getString(1)
                                                + " Longitude");

                                    }

                                    st.close();
                                    sqliteDB.close();
                                }

                                catch (Exception e) {
                                    System.out.println(e.getMessage()
                                            + " wut");
                                    e.printStackTrace();
                                }
                                try {
                                    URI uri = URI
                                            .create("file:///SDCard/"
                                                    + "database1.db");
                                    sqliteDB = DatabaseFactory.open(uri);
                                    Statement st = null;
                                    st = sqliteDB
                                            .createStatement("SELECT Name FROM People");
                                    st.prepare();
                                    Cursor c = st.getCursor();
                                    Row r;
                                    int i = 0;
                                    while (c.next()) {
                                        r = c.getRow();
                                        i++;
                                        username=r.getString(0);

                                        System.out.println(r.getString(0)
                                                + "Name");


                                    }

                                    st.close();
                                    sqliteDB.close();
                                }
                                catch(Exception e)
                                {
                                    e.printStackTrace();
                                }
                                SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
                                rpc.addProperty("Username", username);
                                rpc.addProperty("latitude", String.valueOf(lat));
                                rpc.addProperty("longitude", String.valueOf(lon));
                                rpc.addProperty("distance", "1.5");
                                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                                        SoapEnvelope.VER11);

                                envelope.bodyOut = rpc;
                                envelope.dotNet = true;
                                envelope.encodingStyle = SoapSerializationEnvelope.XSD;

                                HttpTransport ht = new HttpTransport(URL);
                                ht.debug = true;

                                try {
                                    ht.call(SOAP_ACTION, envelope);
                                    System.out.println("IN TRY");
                                    SoapObject resultProperties = (SoapObject) envelope
                                            .getResponse();

                                    System.out.println("username INT RIGHT HERE " + resultProperties.getProperty(0));
                                    System.out.println("username INT RIGHT HERE " + resultProperties.getProperty(1).toString());
                                    System.out.println("username INT RIGHT HERE " + resultProperties.getProperty(2).toString());
                                    System.out.println("lat and lon PARSE HERE " + lat+"\n"+lon);

                                    /* here is the notification code */

                                    //ApplicationIndicatorRegistry reg = ApplicationIndicatorRegistry.getInstance();    

                                    //EncodedImage image = EncodedImage.getEncodedImageResource("logosmall.png");

                                    //ApplicationIcon icon = new ApplicationIcon( image );

                                    //ApplicationIndicator indicator = reg.register( icon, false, true);
                                    //indicator.setIcon(icon);
                                    //indicator.setVisible(true);

                                    setupIndicator();

                                    /* end notification code */


                                } catch (org.xmlpull.v1.XmlPullParserException ex2) {

                                } catch (Exception ex) {

                                    String bah = ex.toString();

                                }
                            }

                            private void updateFields() {
                                // TODO Auto-generated method stub
                                try {

                                    URI myURI = URI
                                            .create("file:///SDCard/"
                                                    + "database3.db");
                                    sqliteDB = DatabaseFactory.open(myURI);
                                    Statement st = null;
                                    Statement oops = null;
                                    st = sqliteDB
                                    .createStatement("SELECT Latitude,Longitude FROM Location");
                            st.prepare();
                            Cursor c = st.getCursor();
                            Row r;
                            int i = 0;
                            while (c.next()) {
                                r = c.getRow();
                                i++;
                                x=Double.parseDouble(r.getString(0));
                                y=Double.parseDouble(r.getString(1));
                                System.out.println(r.getString(0)
                                        + " Latitude in update fields");
                                System.out.println(r.getString(1)
                                        + " Longitude in update fields");

                            }
                                    st = sqliteDB
                                            .createStatement("UPDATE Location SET Latitude='"
                                                    + lat
                                                    + "' "
                                                    + "WHERE Latitude="
                                                    + "'" + x + "'" + "");
                                    oops = sqliteDB
                                            .createStatement("UPDATE Location SET Longitude='"
                                                    + lon
                                                    + "' "
                                                    + "WHERE Longitude="
                                                    + "'" + y + "'" + "");
                                    System.out.println("location updated");
                                    System.out
                                            .println("lat and lon values are"
                                                    + lat + lon);
                                    st.prepare();
                                    oops.prepare();
                                    st.execute();
                                    oops.execute();
                                    st.close();
                                    oops.close();
                                    sqliteDB.close();

                                } catch (Exception e) {
                                    System.out.println(e.getMessage());
                                    e.printStackTrace();
                                }

                            }
                        });
            }

        }
    }
}

public void providerStateChanged(LocationProvider provider, int newState) {
    if (newState == LocationProvider.OUT_OF_SERVICE) {
        // GPS unavailable due to IT policy specification
        System.out.println("GPS unavailable due to IT policy specification");
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run() {
                Dialog.alert("GPS unavailable due to IT policy specification");

                return;
            }
        });
    } else if (newState == LocationProvider.TEMPORARILY_UNAVAILABLE) {
        // no GPS fix
        System.out.println("GPS temporarily unavailable due to IT policy specification");
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run() {
                Dialog.alert("no GPS fix");
                return;
            }
        });
    }

}


public ApplicationIndicator _indicator; 
public static handleGPSListener _instance;

public void setupIndicator() {

    //Setup notification 
    if (_indicator == null) {
        ApplicationIndicatorRegistry reg = ApplicationIndicatorRegistry.getInstance();
        _indicator = reg.getApplicationIndicator();

        if(_indicator == null) {
            ApplicationIcon icon = new ApplicationIcon(EncodedImage.getEncodedImageResource ("daslogo.png"));
            _indicator = reg.register(icon, false, true);  
            _indicator.setValue(0);
            _indicator.setVisible(false);
        }
    }

}

public void setVisible(boolean visible, int count) {

    if (_indicator != null) {
        if (visible) {
            _indicator.setVisible(true);
            _indicator.setValue(count);
        } else {
            _indicator.setVisible(false);
        }
    }

}

handleGPSListener () {

}

public static handleGPSListener getInstance() {
    if (_instance == null) {
        _instance = new handleGPSListener ();
    }
    return(_instance);
}

}

根据我的理解,我可以使用此代码,但是当我这样做时,请说Demo message could not be resolved as a type

     //Register the ApplicationMessageFolder
    //ReadableListImpl source is available in the messagelistdemo.
       ApplicationMessageFolder folder =       ApplicationMessageFolderRegistry.getInstance().registerFolder(
0x33c7ce29883abe5fL, "Test Folder", new ReadableListImpl());

    //DemoMessage source is available in the messagelistdemo.
      DemoMessage msg = new DemoMessage("me@here.com", "Pizza Toppings", 
"What would you like on your pizza?", System.currentTimeMillis());
      folder.fireElementAdded(msg);

    //Display the application indicator icon.
      ApplicationIndicatorRegistry reg = ApplicationIndicatorRegistry.getInstance();    

    EncodedImage image = EncodedImage.getEncodedImageResource("Indicator.png");

    ApplicationIcon icon = new ApplicationIcon(image);

    ApplicationIndicator indicator = reg.register(icon, false, true);
    indicator.setIcon(icon);
    indicator.setVisible(true);

1 个答案:

答案 0 :(得分:1)

我认为你不能点击图标带你进入应用程序。我的理解是它只适用于像facebook等建立的应用程序。

但要显示指标试试这个

import net.rim.blackberry.api.messagelist.ApplicationIcon;
import net.rim.blackberry.api.messagelist.ApplicationIndicator;
import net.rim.blackberry.api.messagelist.ApplicationIndicatorRegistry;
import net.rim.device.api.system.EncodedImage; 

public class MyAppIndicator{


    public ApplicationIndicator _indicator; 
    public static MyAppIndicator _instance; 
    private MyAppIndicator () {

    }

    public static MyAppIndicator getInstance() {
        if (_instance == null) {
            _instance = new MyAppIndicator ();
        }
        return(_instance);
    }

    public void setupIndicator() {

        //Setup notification 
        if (_indicator == null) {
            ApplicationIndicatorRegistry reg = ApplicationIndicatorRegistry.getInstance();
            _indicator = reg.getApplicationIndicator();

            if(_indicator == null) {
                ApplicationIcon icon = new ApplicationIcon(EncodedImage.getEncodedImageResource ("status_icon_24x24.png"));
                _indicator = reg.register(icon, false, true);  
                _indicator.setValue(0);
                _indicator.setVisible(false);
            }
        }

    }

    public void setVisible(boolean visible, int count) {

        if (_indicator != null) {
            if (visible) {
                _indicator.setVisible(true);
                _indicator.setValue(count);
            } else {
                _indicator.setVisible(false);
            }
        }

    }

}