2

私のソース コードには、AlarmManager の 2 つのインスタンスが組み込まれています。テキストビューには文字列の正しい時間が表示されます: time ただし、2 番目の AlarmManager のアクティビティ (KillTimer.java) を開始するようには見えず、その理由はわかりません。単純なものを見落としていると確信していますが、それが何であるかはわかりません。

PS

時間文字列を実装した方法に関係している可能性があると思いますが、よくわかりません。

時間文字列は、次の行で 1000 を掛けることができるものを表す必要があります。

時間 != null ? 1000 : 0、ピンテント 2);

ただし、ソースコードはそれを認識せず、時間の文字列を追加する前のようにアラームがアクティビティを起こすことはありません。

コード スニペット: (文字列の値を使用して開始されないアラーム: time)

 // Start 2nd service using AlarmManager


    Intent intent2 = new Intent(Rules.this, KillTimer.class);
    PendingIntent pintent2 = PendingIntent.getActivity(Rules.this, 0, intent2,
            0);
    AlarmManager alarm2 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarm2.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
            (time != null ? 1000 : 0), pintent2);   

完全なソース:

public class Rules extends Activity {
    private String password;
    private  PendingIntent mPendingIntent;
    String TIMELIMIT = "10";


TextView textSsid, textSpeed, textRssi, Time;
//Notification message ID
private static final int NOTIFY_ME_ID=1337;

private int count=0;
private NotificationManager notifyMgr=null;
    public Handler mHandler = new Handler();
    public long mStartRX = 0;
    public long mStartTX = 0;
    public long txBytes;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.rules);

    String NDEF_PREF = "prefs";
    SharedPreferences prefs = getSharedPreferences(NDEF_PREF, Context.MODE_PRIVATE); 
    String name = prefs.getString("name", ""); 
    String code = prefs.getString("corename", "");

    String time = prefs.getString("time", "");
    String ssid = prefs.getString("restricted", "");
    Time = (TextView) findViewById(R.id.Time);
    Time.setText(time);


   Parse.initialize(this, "7gjqmUcoqu1IZPJSSxXLdE4L8efAugCXA7snLSH6", "5NckF83MUBumQ8L8zL7Akc4p07beMRnmvgCfhZdH");

    ParseUser.enableAutomaticUser();
    ParseACL defaultACL = new ParseACL();


    defaultACL.setPublicReadAccess(true);

    ParseACL.setDefaultACL(defaultACL, true);

textSsid = (TextView) findViewById(R.id.Ssid);
textSpeed = (TextView) findViewById(R.id.Speed);
textRssi = (TextView) findViewById(R.id.Rssi);
Time = (TextView) findViewById(R.id.Time);
Long.toString(mStartTX);
Long.toString(mStartRX);
Long.toString(txBytes);    
ParseAnalytics.trackAppOpened(getIntent());



mStartRX = TrafficStats.getTotalRxBytes();
mStartTX = TrafficStats.getTotalTxBytes();
if (mStartRX == TrafficStats.UNSUPPORTED || mStartTX == TrafficStats.UNSUPPORTED) {
    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("Uh Oh!");
    alert.setMessage("Your device does not support traffic stat monitoring.");
    alert.show();
} else {
    mHandler.postDelayed(mRunnable, 1000);
}

}

private final Runnable mRunnable = new Runnable() {
public void run() {
    TextView RX = (TextView)findViewById(R.id.RX);      TextView TX = (TextView)findViewById(R.id.TX);

        long rxBytes = TrafficStats.getTotalRxBytes()- mStartRX;
        RX.setText(Long.toString(rxBytes));
        long txBytes = TrafficStats.getTotalTxBytes()- mStartTX;
        TX.setText(Long.toString(txBytes));
        mHandler.postDelayed(mRunnable, 1000);


        final Chronometer myChronometer = (Chronometer)findViewById(R.id.chronometer);
        myChronometer.start();



        DisplayWifiState();
        this.registerReceiver(this.myWifiReceiver, new IntentFilter(
                ConnectivityManager.CONNECTIVITY_ACTION));

    }

    private void registerReceiver(BroadcastReceiver myWifiReceiver2,
            IntentFilter intentFilter) {
        // TODO Auto-generated method stub

    }

    private BroadcastReceiver myWifiReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            // TODO Auto-generated method stub
            NetworkInfo networkInfo = (NetworkInfo) arg1
                    .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                DisplayWifiState();
            }
        }
    };

    public void DisplayWifiState() {

        ConnectivityManager myConnManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo myNetworkInfo = myConnManager
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        WifiManager myWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        WifiInfo myWifiInfo = myWifiManager.getConnectionInfo();

        if (myNetworkInfo.isConnected()) {

            textSsid.setText(myWifiInfo.getSSID());


            textSpeed.setText(String.valueOf(myWifiInfo.getLinkSpeed()) + " "
                    + WifiInfo.LINK_SPEED_UNITS);
            textRssi.setText(String.valueOf(myWifiInfo.getRssi()));
        } else {
            textSsid.setText("---");

            textSpeed.setText("---");
            textRssi.setText("---");
        };

    // Start service using AlarmManager

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, 10);
        Intent intent = new Intent(Rules.this, LMW.class);
        PendingIntent pintent = PendingIntent.getService(Rules.this, 0, intent,
                0);
        AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                7 * 1000, pintent);



        String NDEF_PREF = "prefs";
        SharedPreferences prefs = getSharedPreferences(NDEF_PREF, Context.MODE_PRIVATE); 
        String name = prefs.getString("name", ""); 
        String code = prefs.getString("corename", "");
        String time = prefs.getString("time", "");
        String ssid = prefs.getString("restricted", "");

        //String time = String.valueOf(time);




        // Start 2nd service using AlarmManager


        Intent intent2 = new Intent(Rules.this, KillTimer.class);
        PendingIntent pintent2 = PendingIntent.getActivity(Rules.this, 0, intent2,
                0);
        AlarmManager alarm2 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarm2.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                (time != null ? 1000 : 0), pintent2);   




    // click listener for the button to start service
    Button btnStart = (Button) findViewById(R.id.button1);
    btnStart.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            startService(new Intent(getBaseContext(), LMW.class));            
            Intent startMain = new Intent(Intent.ACTION_MAIN);
            startMain.addCategory(Intent.CATEGORY_HOME);
            startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(startMain);



        }        

});

    // click listener for the button to stop service
    Button btnStop = (Button) findViewById(R.id.button2);
    btnStop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            stopService(new Intent(getBaseContext(), LMW.class));
            Intent startMain = new Intent(Intent.ACTION_MAIN);
            startMain.addCategory(Intent.CATEGORY_HOME);
            startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(startMain);

        }
    });





}};}

キルタイマー.JAVA

public class KillTimer extends Activity {

    @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.killtimer);
      Toast.makeText(getApplicationContext(), "KillWifi Running!", Toast.LENGTH_SHORT).show();
      WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
      int networkId = wifiManager.getConnectionInfo().getNetworkId();
      wifiManager.removeNetwork(networkId );
      wifiManager.saveConfiguration();

  }}

ブロードキャスト レシーバ ソース:

import java.util.List;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class StartKillTimerReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        startActivity(context, KillTimer.class);
    }
}
4

2 に答える 2

1
PendingIntent.getActivity

する必要があります

PendingIntent.getService

説明する必要はありません。ここで違いを理解していると思います。簡単です。

編集:

で開始するActivity場合AlarmManagerは、 を作成しBroadcastReceiverて メソッドを使用する必要がありますPendingIntent.getBroadcast。次に、内部Receiverで呼び出す必要がありますstartActivity(context, KillTimer.class);

を実装していReceiverます。また、次のように を に追加する必要がありReceiverます。Manifest<receiver android:name=".StartKillTimerReceiver" />

public class StartKillTimerReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        context.startActivity(context, KillTimer.class);
    }
}

Receiverこれには 1 つの作業しかないことを忘れないでください。それはKillTimer. Receiver今後は、複数のことを実行できるを作成する方がよいでしょう。したがって、別のアクティビティなどを開始したい場合は、 でデータも送信する必要がありますIntent

于 2013-04-22T09:05:19.723 に答える