工作在4.0+但在3.0之前不工作的通知

时间:2014-01-15 11:14:58

标签: android

我不知道为什么它会起作用。我已经在4.0+上测试了它就像我想要的那样工作。但在2.3.3它没有。我收到IllegalArgumentException,说“需要contentIntent:pkg = com.omerfirmak.btcturktracker” 编辑:在nm.notify(1,noti)调用

上发生错误
package com.omerfirmak.btcturktracker;

import java.io.BufferedReader;
....

public class Alarm extends AsyncTask<String, Void, String>{
    private Notification noti;
    private NotificationManager nm;
    PendingIntent contentIntent;
    protected void onPreExecute(String... params) {
        Intent notificationIntent = new Intent(MainActivity.mContext.getApplicationContext(), MainActivity.class);
        contentIntent = PendingIntent.getActivity(MainActivity.mContext.getApplicationContext(),
                0, notificationIntent,
                PendingIntent.FLAG_CANCEL_CURRENT);
    }

    protected String doInBackground(String... params) {     
    if(MainActivity.alarmToggle.isChecked()){
        while(true){
        try{    
            StringBuilder builder = new StringBuilder();
            HttpClient client = new DefaultHttpClient();
            HttpGet requestedUrl = new HttpGet("http://www.btcturk.com/api/ticker");
            HttpResponse response;
            response = client.execute(requestedUrl);        
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream inputStream = entity.getContent();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                for (String line = null; (line = bufferedReader.readLine()) != null;) {
                    builder.append(line).append("\n");
                }
            }
            Map<String, Double> map = new Gson().fromJson(builder.toString(), new TypeToken<HashMap<String, Double>>() {}.getType());
            if(map.get("last") <= MainActivity.dusukLimitVal && MainActivity.dusukLimitVal != 0){           
                noti = new NotificationCompat.Builder(MainActivity.mContext)
                .setContentIntent(contentIntent)
                .setContentTitle("Düşük Fiyat Uyarısı")
                .setContentText("Bitcoinin fiyatı belirlediğiniz fiyatın altına düştü.")
                .setSmallIcon(R.drawable.ic_launcher)
                .setTicker("BTCTürk Düşük Fiyat Uyarısı")
                .build();       
                noti.defaults = Notification.DEFAULT_ALL;
                    nm = (NotificationManager) MainActivity.mContext.getSystemService(Context.NOTIFICATION_SERVICE);
                    nm.notify(1, noti);
            }   
            if(map.get("last") >= MainActivity.yuksekLimitVal && MainActivity.yuksekLimitVal != 0){         
                noti = new NotificationCompat.Builder(MainActivity.mContext)
                .setContentIntent(contentIntent)
                .setContentTitle("Yüksek Fiyat Uyarısı")
                .setContentText("Bitcoinin fiyatı belirlediğiniz fiyatın üstüne çıktı.")
                .setSmallIcon(R.drawable.ic_launcher)
                .setTicker("BTCTürk Yüksek Fiyat Uyarısı")
                .build();
                noti.defaults = Notification.DEFAULT_ALL;
                    nm = (NotificationManager) MainActivity.mContext.getSystemService(Context.NOTIFICATION_SERVICE);
                    nm.notify(2, noti);                 
            }           
        }catch(Exception e){
            e.printStackTrace();
        }

        try {
            Thread.sleep(600000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if (isCancelled()) break;
        }
    }
        return null;
    }

}

1 个答案:

答案 0 :(得分:0)

有一个名为NotificationCompat的类

import android.support.v4.app.NotificationCompat;

通知noti = new NotificationCompat.Builder(ctx)...

将适用于所有设备2.2或2.1我完全相信,这也是我们正在使用的