2016-11-15 15 views
0

私は、時間のかかるAndroidアプリケーションをtimePickerで実行しました。選択した時刻が現在の時刻と同じ場合、アプリケーションからお知らせ。これはアンドロイドスタジオで書いたコードです:timePickerから時刻を取得して現在の時刻で確認する際の問題

public Button mb; 
public EditText editText; 
public Button pb; 
int i=0; 

public void showMotivation(){ 

    mb = (Button)findViewById(R.id.mb); 
    editText = (EditText)findViewById(R.id.editText); 

    mb.setOnClickListener(
      new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        editText.setText(MotivationWords()); 
       } 
      } 
    ); 

} 

public String MotivationWords(){ 

    Random rnd = new Random(); 
    List l = new LinkedList<>(); 
    l.add("The price of excellence is discipline; the cost of mediocrity is disappointment."); 
    l.add("If we had no winter, the spring would not be so pleasant; if we did not sometimes taste of adversity, prosperity would not be so welcome."); 
    l.add("If your ship doesn’t come in, swim out to it!"); 
    l.add("Tomorrow will be a better day .."); 
    l.add("Life’s not about waiting for the storms to pass... it’s about learning to dance in the rain."); 
    l.add("When you reach the end of your rope, tie a knot in it and hang on."); 
    l.add("A smooth sea never made a skilled mariner."); 
    l.add("Great masters merit emulation, not worship."); 
    l.add("Confidence is contagious. So is the lack of confidence."); 
    l.add("Optimism may sometimes be delusional, but pessimism is always delusional."); 

    i=rnd.nextInt()%9; 

    return l.get(i).toString(); 


} 

public void changing(){ 

    pb = (Button)findViewById(R.id.pb); 
    pb.setOnClickListener(
      new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 

        Intent intb = new Intent(MainActivity.this, Main2Activity.class); 
        startActivity(intb); 
       } 
      } 
    ); 

} 


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


    changing(); 
    showMotivation(); 
} 

答えて

0
public Button tb; 
public Button bb; 
public TimePicker timep; 
public int h,m; 
public int curh, curm; 
public Calendar c; 
NotificationCompat.Builder mbuilder = new NotificationCompat.Builder(this); 
public int i; 



public void change2(){ 

    bb = (Button)findViewById(R.id.bb); 
    bb.setOnClickListener(
      new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 

        Intent intbb = new Intent(Main2Activity.this, MainActivity.class); 
        startActivity(intbb); 
       } 
      } 
    ); 

} 

public String MotivationWords(){ 

    Random rnd = new Random(); 
    List l = new LinkedList<>(); 
    l.add("The price of excellence is discipline; the cost of mediocrity is disappointment."); 
    l.add("If we had no winter, the spring would not be so pleasant; if we did not sometimes taste of adversity, prosperity would not be so welcome."); 
    l.add("If your ship doesn’t come in, swim out to it!"); 
    l.add("Tomorrow will be a better day .."); 
    l.add("Life’s not about waiting for the storms to pass... it’s about learning to dance in the rain."); 
    l.add("When you reach the end of your rope, tie a knot in it and hang on."); 
    l.add("A smooth sea never made a skilled mariner."); 
    l.add("Great masters merit emulation, not worship."); 
    l.add("Confidence is contagious. So is the lack of confidence."); 
    l.add("Optimism may sometimes be delusional, but pessimism is always delusional."); 

    i=rnd.nextInt()%9; 

    return l.get(i).toString(); 


} 

public void takeTime(){ 
    timep = (TimePicker)findViewById(R.id.timep); 

    timep.setOnTimeChangedListener(
      new TimePicker.OnTimeChangedListener() { 
       @Override 
       public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { 

        h = hourOfDay; 
        m = minute; 
        curh = c.get(Calendar.HOUR); 
        curm = c.get(Calendar.MINUTE); 

        if((h==curh) && (m == curm)){ 
         mbuilder.setContentTitle("Your Motivation For now :"); 
         mbuilder.setContentText(MotivationWords()); 
        } 
       } 
      } 
    ); 

} 


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

    change2(); 
    tb = (Button)findViewById(R.id.tb); 
    tb.setOnClickListener(
      new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        takeTime(); 
       } 
      } 
    ); 

} 
関連する問題