使用android

时间:2016-09-27 07:09:05

标签: android json

我能够以正常方式发送短信。这是我的代码:

public class MainActivity extends Activity {
    // Log tag
    private static final String TAG = MainActivity1.class.getSimpleName();

    // Movies json url
    private static final String url = "http://xxx.xxx.x.xx/sms/read_file.php";
    private ProgressDialog pDialog;
    private List<SmsClass> movieList = new ArrayList<SmsClass>();
    private ListView listView;
    private CustomListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView) findViewById(R.id.list);
        adapter = new CustomListAdapter(this, movieList);
        listView.setAdapter(adapter);

        pDialog = new ProgressDialog(this);
        pDialog.setMessage("Loading...");
        pDialog.show();

        // Creating volley request obj
        JsonArrayRequest movieReq = new JsonArrayRequest(url,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());
                        hidePDialog();

                        // Parsing json
                        for (int i = 0; i < response.length(); i++) {
                            try {
                                JSONObject obj = response.getJSONObject(i);
                                SmsClass movie = new SmsClass();


                                movie.setTitle(obj.getString("contents"));
                                movie.setRating(obj.getString("mobno"));
                                movieList.add(movie);

                                String phoneNo = "9876543210";
                                String msg = obj.getString("contents");                        
                                sendSMS(phoneNo, msg); //working perfect without monitoring

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                        adapter.notifyDataSetChanged();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                hidePDialog();
            }
        });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(movieReq);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        hidePDialog();
    }

    private void hidePDialog() {
        if (pDialog != null) {
            pDialog.dismiss();
            pDialog = null;
        }
    }
    //working perfect
    private void sendSMS(String phoneNumber, String message) {
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, null, null);
    }
}

这里从json数据中获取消息体并发送sms。如何在后台执行相同的功能而不需要任何用户界面,即使用服务?

1 个答案:

答案 0 :(得分:1)

在应用程序标记中声明服务清单文件。

   <application
       android:icon="@drawable/ic_launcher"
       android:label="@string/app_name"
       android:theme="@style/AppTheme" >

       <service android:name=".MyService" />

   </application>

<强> MainActivity.java

  public class MainActivity extends Activity {

       @Override
       public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);

          startService();
       }


       // Method to start the service
       public void startService() {
          startService(new Intent(getBaseContext(), MyService.class));
       }

       // Method to stop the service
       public void stopService() {
          stopService(new Intent(getBaseContext(), MyService.class));
       }
    }

<强> MyService.java

public class MyService extends Service {

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

   @Override
   public int onStartCommand(Intent intent, int flags, int startId) {
      // Let it continue running until it is stopped.
      Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
      String phoneNo = "9876543210";
      String msg = obj.getString("contents");                        
      sendSMS(phoneNo, msg);
      return START_STICKY;
   }

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

   private void sendSMS(String phoneNumber, String message) {
      SmsManager sms = SmsManager.getDefault();
      sms.sendTextMessage(phoneNumber, null, message, null, null);
   }
}