2016-10-04 3 views
-1
public class AndroidServiceStartOnBoot extends Service { 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 


    @Override 
    public void onCreate() { 
     super.onCreate(); 

    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
      showToast(); 
     return START_REDELIVER_INTENT; 
    } 


    private Runnable mRunnable = new Runnable() { 
     @Override 
     public void run() { 

      showToast(); 


     } 
    }; 



    private void showToast() { 
     logInRequest(); 
     int icon = R.drawable.nicon; 
     long when = System.currentTimeMillis(); 
     Intent intent = new Intent(); 
     PendingIntent pendingIntent = PendingIntent.getActivities(AndroidServiceStartOnBoot.this, 0, new Intent[]{intent}, 0); 
     Notification notification = new Notification(icon, "Custom Notification", when); 


     NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

     RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification); 
     contentView.setImageViewResource(R.id.image, R.drawable.nicon); 
     contentView.setTextViewText(R.id.title, "Custom notification"); 
     contentView.setTextViewText(R.id.text, status); 
     contentView.setOnClickPendingIntent(R.id.closeOnFlash, pendingIntent); 
     notification.contentView = contentView; 

     Intent notificationIntent = new Intent(this, MainActivity.class); 
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
     notification.contentIntent = contentIntent; 

     notification.flags = Notification.FLAG_AUTO_CANCEL; //Do not clear the notification 
     notification.defaults |= Notification.DEFAULT_LIGHTS; // LED 
     notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration 
     notification.defaults |= Notification.DEFAULT_SOUND; // Sound 

     mNotificationManager.notify(0, notification); 

     new Handler().postDelayed(mRunnable, 4000); 

    } 


    private void logInRequest() { 


     SharedPreferences tprefs = getSharedPreferences("com.nahid.com.gsdambassadorpractice", MODE_PRIVATE); 
     String token = tprefs.getString("token", "false"); 


     String url = Common.baseUrl + "ambassadorcall/api/call/addcard"; 
     Map<String, String> param = new HashMap<String, String>(); 
     param.put("token", token); 
     param.put("cardno", "125847"); 


     JSON2Request json2Request = new JSON2Request(Request.Method.POST, url, param, logInListeneryy()); 
     RequestManager.showProgressDlg(AndroidServiceStartOnBoot.this); 
     RequestManager.addRequest(json2Request, this); 

    } 


    private Response.Listener<JSONObject> logInListeneryy() { 
     return new Response.Listener<JSONObject>() { 
      @Override 
      public void onResponse(JSONObject response) { 
       RequestManager.dismissProgressDlg(); 

       JSONObject jobj = response; 

       try { 
        if (jobj.getString("status").equals("success")) { 


         status = jobj.getString("status"); 

         Toast.makeText(AndroidServiceStartOnBoot.this, "Working", Toast.LENGTH_LONG).show(); 

         //String redirect =stepManage.gotostep(LoginActivity.this,jobj.getString("nextstep")); 


/** 
String token = tprefs.getString("token", "false"); 
Toast.makeText(LoginActivity.this, token, Toast.LENGTH_LONG).show(); 
**/ 


        } else { 
         Toast.makeText(AndroidServiceStartOnBoot.this, jobj.getString("message"), Toast.LENGTH_LONG).show(); 
        } 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 
     }; 
    } 


    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show(); 
    } 

} 

通知マネージャを使用してカスタム通知を表示するサービスを作成したいと思いますが、サービスクラスからapiを使用して通知を更新したいと思います。どのように私はアンドロイドでそれを行うことができます私を助けてください。アンドロイドのサービスクラスを使用してAPIを呼び出して通知する

+1

これまで行ってきたこと – Anjali

+0

loginrequest()関数を使用して私のコードを.please help now.withに投稿しました。私はapiとloginListneryy()を呼び出しています。その通知の結果。 – Nahid

答えて

2

これは、バックグラウンドサービスを常に実行して、いくつかの条件で通知を表示することができます。私のアプリケーションと同様に、通知用のサービスとクラスがあります。特定の条件では、私のサービスから通知クラスを呼び出します。

+0

はい、バックグラウンドサービスを使用しています。電話が起動している間にサービスが開始されます。しかし、私が望むのは、特定の時間間隔の後にapiを呼び出してからjsonデータを解析する場合です。しかし、私はすべてサービスクラスから起こりたい。あなたが私を助けてくれたら。 – Nahid

+0

これはタイマーを使うことができます。タイマーを使用して、時間を設定できます。そして、あなたはあなたのAPIを呼び出すことができ、ユーザーに通知することができます。 –

+0

サービスクラスで私はどこで私のAPIを使用するのか知りたいです。 onStartCommandメソッドまたはonHandleIntentメソッドです。私がサンプルコードを与えると非常にうれしいです – Nahid

関連する問題