
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
SharedPreferences prefs = context.getSharedPreferences("MyWidget", Context.MODE_PRIVATE);
int number = prefs.getInt("number", 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.stop_watch);
views.setTextViewText(R.id.timer, String.valueOf(number));
Intent playIntent = new Intent(context, StopWatch.class);
playIntent.setAction("PLAY_ACTION");
PendingIntent playPendingIntent = PendingIntent.getBroadcast(context, 0, playIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
views.setOnClickPendingIntent(R.id.playButton, playPendingIntent);
Intent stopIntent = new Intent(context, StopWatch.class);
stopIntent.setAction("STOP_ACTION");
PendingIntent stopPendingIntent = PendingIntent.getBroadcast(context, 1, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
views.setOnClickPendingIntent(R.id.stopButton, stopPendingIntent);
Intent resetIntent = new Intent(context, StopWatch.class);
resetIntent.setAction("RESET_ACTION");
PendingIntent resetPendingIntent = PendingIntent.getBroadcast(context, 2, resetIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
views.setOnClickPendingIntent(R.id.resetButton, resetPendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
const playTimer = () => {
StopWatchModule.playTimer();
setIsPlaying(true);
};
const stopTimer = () => {
StopWatchModule.stopTimer();
setIsPlaying(false);
};
const resetTimer = () => {
StopWatchModule.resetTimer();
setWidgetData("0:00:00");
setIsPlaying(false);
};
return (
<View>
...
<Button title="Play" onPress={() => playTimer} />
<Button title="Stop" onPress={() => stopTimer} />
<Button title="Reset" onPress={() => resetTimer} />
</View>
);
@ReactMethod
public void playTimer() {
Intent intent = new Intent(reactContext, StopWatch.class);
intent.setAction("PLAY_ACTION");
reactContext.sendBroadcast(intent);
}
@ReactMethod
public void stopTimer() {
Intent intent = new Intent(reactContext, StopWatch.class);
intent.setAction("STOP_ACTION");
reactContext.sendBroadcast(intent);
}
@ReactMethod
public void resetTimer() {
Intent intent = new Intent(reactContext, StopWatch.class);
intent.setAction("RESET_ACTION");
reactContext.sendBroadcast(intent);
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
SharedPreferences prefs = context.getSharedPreferences("MyWidget", Context.MODE_PRIVATE);
boolean isRunning = prefs.getBoolean("isRunning" , false);
Log.d("StopWatch", "Received action: " + intent.getAction());
if ("PLAY_ACTION".equals(intent.getAction())) {
if (!isRunning) {
prefs.edit().putBoolean("isRunning", true).apply();
if (handler == null) {
handler = new MyHandler(context);
}
handler.sendMessage(handler.obtainMessage(0));
}
} else if ("STOP_ACTION".equals(intent.getAction())) {
prefs.edit().putBoolean("isRunning", false).apply();
if (handler != null) {
handler.removeMessages(0);
}
} else if ("RESET_ACTION".equals(intent.getAction())) {
prefs.edit().putBoolean("isRunning", false).apply();
if (handler != null) {
handler.removeMessages(0);
}
prefs.edit().putInt("number", 0).apply();
}
updateAllWidgets(context);
}
static void updateAllWidgets(Context context) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, StopWatch.class));
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
private static class MyHandler extends Handler {
private final WeakReference<Context> contextRef;
MyHandler(Context context) {
super(Looper.getMainLooper());
contextRef = new WeakReference<>(context);
}
@Override
public void handleMessage(Message msg) {
Context context = contextRef.get();
if (context == null) return;
SharedPreferences prefs = context.getSharedPreferences("MyWidget", Context.MODE_PRIVATE);
int number = prefs.getInt("number", 0);
boolean isRunning = prefs.getBoolean("isRunning", false);
prefs.edit().putInt("number", number + 1).apply();
updateAllWidgets(context);
WritableMap map = Arguments.createMap();
map.putInt("number", number + 1);
StopWatchModule.emitDeviceEvent("onAppWidgetUpdate", map);
if (isRunning) {
this.sendMessageDelayed(this.obtainMessage(0), 1000);
}
}
}
@ReactMethod
public void getNumber(Promise promise) {
try {
SharedPreferences prefs = StopWatchModule.reactContext.getSharedPreferences("MyWidget", Context.MODE_PRIVATE);
int number = prefs.getInt("number", 0);
int hours = number / 3600;
int minutes = (number % 3600) / 60;
int seconds = number % 60;
String formattedTime = String.format("%01d:%02d:%02d", hours, minutes, seconds);
promise.resolve(formattedTime);
} catch (Exception e) {
promise.reject("GET_NUMBER_ERROR", e);
}
}

만들고 보니 디자인이 막 그리 이쁘진 않다,, ㅠㅠ 일단 잘 작동하니 백엔드 api 연결하고 다시 보자