Last active
December 21, 2015 06:08
-
-
Save lacolaco/6261569 to your computer and use it in GitHub Desktop.
Androidに定時アイコン変更を自動的にさせるプログラム
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
import java.util.Timer; | |
import java.util.TimerTask; | |
import android.app.Activity; | |
import android.app.ActivityManager; | |
import android.app.ActivityManager.RunningServiceInfo; | |
import android.app.AlarmManager; | |
import android.app.PendingIntent; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.widget.TextView; | |
public class MainActivity extends Activity implements OnClickListener | |
{ | |
@Override | |
protected void onCreate(Bundle savedInstanceState) | |
{ | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
//View部分なのでロジックに関係なし | |
View start = findViewById(R.id.btn_start); | |
View stop = findViewById(R.id.btn_stop); | |
start.setOnClickListener(this); | |
stop.setOnClickListener(this); | |
} | |
@Override | |
public void onClick(View v) | |
{ | |
//Serviceの起動準備 | |
Context context = getBaseContext(); | |
Intent intent = new Intent(context, MyService.class); | |
//AndroidOSのアラームマネージャに定期的なサービスの実行を移譲 | |
//常時起動ではなく、OSがその時間にService起動のIntentを飛ばしてくれる | |
PendingIntent pendingIntent = PendingIntent.getService(context, -1, intent, PendingIntent.FLAG_UPDATE_CURRENT); | |
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE); | |
//ボタン処理 | |
if(v.getId() == R.id.btn_start) | |
{ | |
//1000ms後に1時間おきのアラームを始動 | |
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, 1*1000, AlarmManager.INTERVAL_HOUR, pendingIntent); | |
} | |
else if(v.getId() ==R.id.btn_stop) | |
{ | |
//アラーム移譲の停止 | |
alarmManager.cancel(pendingIntent); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Calendar; | |
import java.util.Date; | |
import android.app.IntentService; | |
import android.content.Intent; | |
import android.content.SharedPreferences; | |
import android.preference.Preference; | |
import android.preference.PreferenceManager; | |
import android.util.Log; | |
//定期的に実行されるService実体 | |
public class MyService extends IntentService | |
{ | |
public MyService() | |
{ | |
super("IconRotate"); | |
} | |
@Override | |
public void onDestroy() | |
{ | |
super.onDestroy(); | |
} | |
//起動されたら呼ばれる部分 | |
@Override | |
protected void onHandleIntent(Intent intent) | |
{ | |
Log.i("IconRotation", "CHECKING..."); | |
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this); | |
int lastIcon = pref.getInt("lastIcon", -1); | |
int currentIcon = getIconNum(); | |
if(lastIcon == -1 || lastIcon != currentIcon) | |
{ | |
//今の時間のアイコンと現在使ってるアイコンが違ったら変更 | |
//アイコンファイルの名前を生成 | |
String name = String.format("%02d.png", currentIcon); | |
//Uploaderにアイコンアップロードを移譲 | |
if(new Uploader(name).upload()) | |
{ | |
pref.edit().putInt("lastIcon", currentIcon).commit(); | |
} | |
} | |
} | |
//現在の時間から使用するアイコンのナンバーを返す | |
int getIconNum() | |
{ | |
Calendar calendar = Calendar.getInstance(); | |
calendar.setTime(new Date()); | |
int hour = calendar.get(Calendar.HOUR_OF_DAY); | |
if(hour >= 18 || hour <= 6) | |
{ | |
return 3; //night | |
} | |
else if(hour >= 10) | |
{ | |
return 2; //noon-afternoon | |
} | |
else | |
{ | |
return 1; //morning | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import twitter4j.Twitter; | |
import twitter4j.TwitterFactory; | |
import twitter4j.auth.AccessToken; | |
import twitter4j.conf.Configuration; | |
import twitter4j.conf.ConfigurationBuilder; | |
public class Uploader | |
{ | |
final static String consumerKey = "lacolaco"; | |
final static String consumerSecret = "lacolaco"; | |
final static String accessToken = "lacolaco"; | |
final static String accessTokenSecret = "lacolaco"; | |
final static String iconUrlBase = "http://dl.dropboxusercontent.com/u/27437828/icons/"; | |
String iconName; | |
public Uploader(String iconName) | |
{ | |
//"01.png"みたいなのが渡される | |
this.iconName = iconName; | |
} | |
public boolean upload() | |
{ | |
//説明めんどうだから読んで | |
InputStream is = null; | |
try | |
{ | |
URL url = new URL(iconUrlBase + iconName); | |
URLConnection conn = url.openConnection(); | |
is = conn.getInputStream(); | |
Twitter twitter = TwitterFactory.getSingleton(); | |
twitter.setOAuthConsumer(consumerKey, consumerSecret); | |
twitter.setOAuthAccessToken(new AccessToken(accessToken, accessTokenSecret)); | |
twitter.updateProfileImage(is); | |
twitter.updateStatus("@laco0416 アイコンを変更します #IconRotation"); | |
is.close(); | |
return true; | |
} | |
catch(Exception e) | |
{ | |
e.printStackTrace(); | |
try | |
{ | |
is.close(); | |
} | |
catch (IOException eio) | |
{ | |
e.printStackTrace(); | |
} | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment