你好,我想打一個應用程序,將檢查每30分鐘背景的互聯網連接,如果是的話,將一些非常小的JSON數據發送到服務器。檢查互聯網連接,每30分鐘
不知道,如果這樣做的權利,但現在我有:
package com.example.lenovotp.sender;
import android.app.AlarmManager;
import android.app.IntentService;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;
import java.util.Calendar;
public class MyClass extends IntentService{
private static final String TAG = "com.example.lenovotp.sender";
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
Calendar calendar = Calendar.getInstance();
public MyClass(){
super("");
}
@Override
protected void onHandleIntent(Intent intent) {
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
intent = new Intent(this, MyClass.class);
PendingIntent alarmIntent = PendingIntent.getService(this, 0, intent, 0);
alarmMgr.set(AlarmManager.RTC_WAKEUP,
1000 * 30, alarmIntent);
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
//boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
if(isConnected!=false){
Toast.makeText(this, "Network is avail!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Network is NOT avail!", Toast.LENGTH_LONG).show();
}
}
}
而且在表現我在最後postition的是
<?xml version="1.0" encoding="utf-8"?>
<!-- Permissions -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyClass"/>
</application>
並在MainActivity
package com.example.lenovotp.sender;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this,MyClass.class);
startService(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
所以,現在我不知道如何:
- 賺的應用程序沒有運行,或者由用戶摧毀它。
- 它每30分鐘檢查一次連接,如果是的話,它會發送一些數據到服務器。
在此先感謝。
修訂
所以你想做什麼?你想發送數據還是要檢查連接? –
我想發送數據。 – comingage
如果時間到了,並且沒有連接,該怎麼辦?你想發送的數據怎麼樣? –